码迷,mamicode.com
首页 > 编程语言 > 详细

python:使用多线程同时执行多个函数

时间:2019-10-17 17:48:37      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:exe   def   pre   col   return   abc   sys   mst   字符   

 

 

使用多线程同时执行多个函数

import time
import os
import threading


def open_calc():
    with open(test.txt, r) as f:
        for line in f.readlines():
            while hello in line:
                os.system("calc.exe")
                # 如果字符串已经出现并已经执行命令,则终止程序,否则会一直执行命令
                return
        # 等for循环判断完没有标识后再休眠重新调用该函数
        print(没有找到启动标识:hello,等5秒再检测)
        time.sleep(5)
        # 再次调用函数
        open_calc()


def open_mstsc():
    with open(test.txt, r) as f:
        for line in f.readlines():
            while abc in line:
                os.system("mstsc.exe")
                # 如果字符串已经出现并已经执行命令,则终止程序,否则会一直执行命令
                return
        # 等for循环判断完没有标识后再休眠重新调用该函数
        print(没有找到启动标识:abc,等6秒再检测)
        time.sleep(6)
        # 再次调用函数
        open_mstsc()


if __name__ == __main__:
    # 使用threading模块,threading.Thread()创建线程,其中target参数值为需要调用的方法,同样将其他多个线程放在一个列表中,遍历这个列表就能同时执行里面的函数了
    threads = [threading.Thread(target=open_calc),
               threading.Thread(target=open_mstsc)]
    for t in threads:
        # 启动线程
        t.start()

 

python:使用多线程同时执行多个函数

标签:exe   def   pre   col   return   abc   sys   mst   字符   

原文地址:https://www.cnblogs.com/gcgc/p/11693047.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!