[ACCEPTED]-Python sleep without interfering with script?-sleep
Interpreting your description literally, you 7 need to put the print statement before the 6 call to func2()
.
However, I'm guessing what you 5 really want is for func2()
to a background task 4 that allows func1()
to return immediately and not 3 wait for func2()
to complete it's execution. In 2 order to do this, you need to create a thread 1 to run func2()
.
import time
import threading
def func1():
t = threading.Thread(target=func2)
t.start()
print("Do stuff here")
def func2():
time.sleep(10)
print("Do more stuff here")
func1()
print("func1 has returned")
You could use threading.Timer
:
from __future__ import print_function
from threading import Timer
def func1():
func2()
print("Do stuff here")
def func2():
Timer(10, print, ["Do more stuff here"]).start()
func1()
But as @unholysampler already pointed out it might be better 1 to just write:
import time
def func1():
print("Do stuff here")
func2()
def func2():
time.sleep(10)
print("Do more stuff here")
func1()
If you're running your script on command 3 line, try using the -u
parameter. It runs 2 the script in unbuffered mode and did the 1 trick for me.
For example:
python -u my_script.py
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.