简体   繁体   English

Python time.sleep()在Linux和多线程中不起作用

[英]Python time.sleep() does not work in linux and multi thread

I write a simple multiprocess and multi-thread code in python which works in windows but doesn't work in linux (i tested it on freebsd and ubuntu) 我在python中编写了一个简单的多进程和多线程代码,该代码在Windows中有效,但在Linux中不起作用(我在freebsd和ubuntu上进行了测试)

import threading
import time
from multiprocessing import Process

class Test(threading.Thread):
    def run(self):
        print('before sleep')
        time.sleep(1)
        print('after sleep')

def run_test():
    Test().start()

if __name__ == "__main__":
    Process(target=run_test, args=()).start() 

this program only print "before sleep" and then exit. 该程序仅在“睡眠前”打印,然后退出。

why sleep doesn't work here? 为什么睡眠在这里不起作用? (it works on windows) (适用于Windows)

UPDATE: 更新:

I used join() in my process like this, but still not work. 我在这样的过程中使用了join(),但仍然无法正常工作。

...

if __name__ == "__main__":
    pr = Process(target=run_test, args=())
    pr.start()
    pr.join()

The join() should be used in the calling thread to wait for another thread: join()应该在调用线程中使用,以等待另一个线程:

def run_test():
    t = Test()
    t.start()
    t.join()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM