Python 3---マルチスレッド同時操作

1441 ワード

'''
   ---                       。
    ---import threading
'''

from time import ctime,sleep
import threading

def read(things,number):
    for i in range(number):
        print("Start Read %s %s"%(things,ctime()))
        sleep(2)


def write(things,number):
    for i in range(number):
        print("Start Write %s %s" % (things, ctime()))
        sleep(2)

threads = []     #       
thd1=threading.Thread(target=read,args=("MIKe",2))  #      
threads.append(thd1)  #      threads    

thd2=threading.Thread(target=write,args=("LAN",2))  #      
threads.append(thd2)

if __name__ == '__main__':
    for th in threads:
        th.start()           #start()---      
    for th in threads:
        th.join()            #join([time])    --       
    print("all the end %r"%ctime())

それについてはjoinの解析
 
'''
  Thread.join         ,              。
  timeout       ,      ,        ,                  
'''
import threading
from time import ctime,sleep
def doWaiting():
    print ('start waiting:', ctime())
    sleep(3)
    print ('stop waiting', ctime())
thread1 = threading.Thread(target = doWaiting)
thread1.start()
sleep(1)
#    thread1    
print ('start join')
print ('now:', ctime())
thread1.join()
#     ,  thread1    。
print ('end join', ctime())