[英]multithreading using python
与python一起使用多线程时,尝试了解以下结果。 以下代码以随机顺序将A和B打印到控制台,这是我想要实现的。 但是第二段代码仅在控制台上显示“ A”,并且永远不会经过t1.start()。 为什么是这样? 我需要执行第二部分代码以使其表现得像第一部分一样吗?
预先感谢,这是我的第一篇文章。
这是我想要的行为:
from threading import Thread
def runA():
while True:
print ('A\n')
def runB():
while True:
print ('B\n')
if __name__ == "__main__":
t1 = Thread(target = runA())
t2 = Thread(target = runB())
t1.setDaemon(True)
t2.setDaemon(True)
t1.start()
t2.start()
while True:
pass
我想要从上面的代码产生的行为,但是要使用下面示例中的类。 以下代码从不执行t2.start()。 为什么是这样?
from threading import Thread
class test():
def runA(self):
while True:
print ('A\n')
def runB(self):
while True:
print ('B\n')
if __name__ == "__main__":
testingNow=test()
t1 = Thread(target = testingNow.runA())
t2 = Thread(target = testingNow.runB())
t1.setDaemon(True)
t2.setDaemon(True)
t1.start()
t2.start()
while True:
pass
摆脱()
在testingNow.runA()
和testingNow.runB()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.