Python Threads集合

时间:2016-08-05 06:57:02

标签: multithreading python-3.x return python-multithreading

我有类似的代码,

import threading

class Mythread(threading.Thread):
        def __init__(self):
            threading.Thread.__init__(self)
        def run(self):
            print('do some processing')

if __name__=='__main__':
       while Ture:
         val = raw_input('next thread')
         t = MyThread()
         t.start()
         t.join()

问题是如何在不阻塞主要功能的情况下继续使用主要功能,因为t.join()会停止主要功能直到t没有完成?

1 个答案:

答案 0 :(得分:0)

你应该把代码放在“代码”标签中,否则它不是真的可读。 你只需要做那样的事情。

if name == 'main':
    #Thread creation
    allThreads = []
    while True:
        val = raw_input('next thread')
        newThread = MyThread()
        newThread.start()
        allThreads.append(newThread)

    #You can do something here

    #Waiting for all threads to stop
    for thread in allThreads: 
        thread.join()