meanme的专栏

join函数

join()会等到调用了该函数的线程执行完后再去执行接下来的代码。比如:

import threadingfrom time import ctime ,sleeploops = [2,4]:print ‘start loop:’,nloop,’at:’,ctime()sleep(nsce)print ‘loop:’,nloop,’at:’,ctime():print ‘start at:’,ctime()threads = []nloops = range(len(loops))for i in nloops:t = threading.Thread(target=loop,args=(i,loops[i]))threads.append(t)i in nloops:threads[i].start()i in nloops:threads[i].join(),ctime()if __name__ == ‘__main__’:main()

上面代码中会等到loop0和loop1执行完其任务后再执行print ’ join ‘。 Once threads are started, they will execute until their given function completes, whereby they will exit. If your main thread has things to do other than wait for threads to complete (such as other processing or waiting for new client requests), it should by all means do so. join() is useful only when you want to wait for thread completion.

threading包的使用:callable function:import threadingfrom time import ctime ,sleeploops = [2,4]:print ‘start loop:’,nloop,’at:’,ctime()sleep(nsce)print ‘loop:’,nloop,’at:’,ctime():print ‘start at:’,ctime()threads = []nloops = range(len(loops))for i in nloops:t = threading.Thread(target=loop,args=(i,loops[i]))threads.append(t)i in nloops:threads[i].start()i in nloops:threads[i].join(),ctime()if __name__ == ‘__main__’:main()

上面代码中,,t = threading.Thread(target=loop,args=(i,loops[i]))是创建了一个线程执行loop函数。args里面是loop函数的有关参数的赋值。

call classes

可是,我却迈不开步伐,怎么也走不出那个圈……

meanme的专栏

相关文章:

你感兴趣的文章:

标签云: