Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

time.sleep hangs multithread function in python

I am having trouble with a sleep statement hanging my multithreading function. I want my function to go about it's buisness while the rest of the program runs. Here is a toy that recreates my problem:

import multiprocessing, sys, time

def f(icount, _sleepTime = 1):
    for i in range(icount):
        time.sleep(_sleepTime)
        print(_sleepTime)

def main(args):
    m = multiprocessing.Process(target = f, args=(4, ))
    m.run()
    # f should be sleeping for 1 second so this print statement should come first
    print(m.is_alive())


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))

can anyone explain why this code outputs:

1
1
1
1
False

instead of:

True
1
1
1
1

#

EDIT

#

I eventually want to run this function on a schedual, and test if it is running before I execute the function. This is an example:

import multiprocessing, sys, time

def f(icount, _sleepTime = 1):
    for i in range(icount):
        time.sleep(_sleepTime)
        print(_sleepTime)

def main(args):
    m = multiprocessing.Process(target = f, args=(4, ))
    for i in range(15):
        time.sleep(.5)
        if not m.is_alive():
            # m.start throws an error after first run
            m.run()
        print("{}".format(m.is_alive()))


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))
like image 538
posop Avatar asked Oct 02 '12 15:10

posop


People also ask

Is time sleep blocking python?

sleep() , your code will need to wait for the Python sleep() call to finish before the thread can exit. The reason you'd want to use wait() here is because wait() is non-blocking, whereas time. sleep() is blocking. What this means is that when you use time.

What is sleep () method in python?

Python sleep() The sleep() function suspends (waits) execution of the current thread for a given number of seconds. Python has a module named time which provides several useful functions to handle time-related tasks. One of the popular functions among them is sleep() .

Does time sleep block all threads?

In a single threaded application, this means everything is blocked while you sleep. In a multithreaded application, only the thread you explicitly 'sleep' will block and the other threads still run within the process.

What is the alternative for time sleep in python?

The Timer is another method available with Threading, and it helps to get the same functionality as Python time sleep. The working of the Timer is shown in the example below: Example: A Timer takes in input as the delay time in Python in seconds, along with a task that needs to be started.


1 Answers

Use start and join instead of run:

import multiprocessing, sys, time

def f(icount, _sleepTime = 1):
    for i in range(icount):
        time.sleep(_sleepTime)
        print(_sleepTime)

def main(args):
    m = multiprocessing.Process(target = f, args=(4, ))
    m.start()
    # f should be sleeping for 1 second so this print statement should come first
    print(m.is_alive())
    m.join()


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))

#

EDIT

#

Again, use start and join instead of run:

import multiprocessing, sys, time

def f(icount, _sleepTime = 1):
    for i in range(icount):
        time.sleep(_sleepTime)
        print(_sleepTime)

def create_process():
    return multiprocessing.Process(target = f, args=(4, ))

def main(args):
    m = create_process()
    m.start()
    for i in range(15):
        time.sleep(.5)
        if not m.is_alive():
            # m.start throws an error after first run
            print("restarting")
            m.join()
            m = create_process()
            m.start()
        print("{}".format(m.is_alive()))
    m.join()


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))
like image 144
Oren Avatar answered Sep 28 '22 07:09

Oren