Foad Tahmasebi
Foad Tahmasebi

Reputation: 1342

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)

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)

UPDATE:

I used join() in my process like this, but still not work.

...

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

Upvotes: 2

Views: 811

Answers (1)

VPfB
VPfB

Reputation: 17237

The join() should be used in the calling thread to wait for another thread:

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

Upvotes: 4

Related Questions