我有一个脚本,我想要一个函数与另一个函数同时运行。在python中创建线程
示例代码我已经看过:
import threading
def MyThread (threading.thread):
doing something........
def MyThread2 (threading.thread):
doing something........
MyThread().start()
MyThread2().start()
我无法得到这个工作。我宁愿使用线程函数而不是类来使用它。
感谢您的任何帮助。
这是工作脚本,感谢所有帮助。
class myClass():
def help(self):
os.system('./ssh.py')
def nope(self):
a = [1,2,3,4,5,6,67,78]
for i in a:
print i
sleep(1)
if __name__ == "__main__":
Yep = myClass()
thread = Thread(target = Yep.help)
thread2 = Thread(target = Yep.nope)
thread.start()
thread2.start()
thread.join()
print 'Finished'
我试过这个。我在上面添加了脚本。你能告诉我如何获得与第一个函数并行的第二个函数。谢谢 – chrissygormley 2010-05-25 15:35:45
@chrissygormley:join()阻塞,直到第一个线程结束。 – FogleBird 2010-05-25 15:40:53
@chrissygormley:如前所述,加入块直到你加入的线程完成,所以在你的情况下,用第二个函数开始第二个线程作为并行运行两个函数的目标,然后可以选择加入其中的一个如果你只是想等到他们完成。 – jkp 2010-05-25 15:45:32