python: using multiple threads simultaneously perform multiple functions

 

 

Use multiple threads simultaneously perform multiple functions

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 " )
                 # If the string has occurred and the command has been executed, the program is terminated, otherwise it will always execute the command 
                return 
        # and other finished not identify the judge for loop and then sleep again call the function 
        Print ( ' no found start identity: hello, like five seconds before detection ' )
        the time.sleep ( 5 )
         # call the function again 
        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 " )
                 # If the string has occurred and the command has been executed, the program is terminated, otherwise it will always execute the command 
                return 
        # and other finished not identify the judge for loop and then sleep again call the function 
        Print ( ' no found start identity: abc, six seconds before detection ' )
        the time.sleep ( 6 )
         # call the function again 
        open_mstsc ()


IF  __name__ == ' __main__ ' :
     # use threading module, threading.Thread () creates a thread, the method in which the target parameter value need to call the same number of other threads in a list, this list can be executed simultaneously iterate function inside the 
    Threads = [of the threading.Thread (target = open_ Calc),
               threading.Thread(target=open_mstsc)]
    for t in threads:
        # 启动线程
        t.start()

 

Guess you like

Origin www.cnblogs.com/gcgc/p/11693047.html