I can't figure out why a Python process would block the main thread. An example:
import multiprocessing
import time
def do_something(name: str):
while True:
print(name)
if __name__ == '__main__':
p1 = multiprocessing.Process(target=do_something, args=(1,))
p1.start()
p2 = multiprocessing.Process(target=do_something, args=(2,))
p2.start()
while True:
print(3)
time.sleep(.1)
The output result of this is a whole bunch of 1 and 2's. While 3 (main process/thread) is never printed. When I remove the time.sleep it starts printing again. Why is it that a time.sleep can make Processes block the main thread?
如果将输出通过管道传输到文本文件,您会发现它确实打印了 3。但是,它大约每 100 毫秒打印一次,而其他两个进程连续打印,因此您可能在控制台中看不到它。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.