【发布时间】:2015-05-25 12:48:42
【问题描述】:
我想创建多进程应用程序。这是示例:
import threading
import time
from logs import LOG
def start_first():
LOG.log("First thread has started")
time.sleep(1000)
def start_second():
LOG.log("second thread has started")
if __name__ == '__main__':
### call birhtday daemon
first_thread = threading.Thread(target=start_first())
### call billing daemon
second_thread = threading.Thread(target=start_second())
### starting all daemons
first_thread.start()
second_thread.start()
在此代码中,第二个线程不起作用。我猜,在first_thread 内调用睡眠函数后,主进程被睡眠了。我找到了this post。但是这里 sleep 是与 class 一起使用的。我明白了(进程以退出代码 0 结束
) 结果,当我运行答案时。谁能解释我在哪里犯了错误?
- 我在 windows 上使用 python 3.*
【问题讨论】:
-
它实际上会运行,但是您在方法 1 中的
time.sleep(1000)太长了,您必须等待 1000 秒,将其更改为time.sleep(5)以查看即时结果。 -
好的,但是它阻塞了第二个线程。为什么会这样?
标签: python multithreading thread-sleep