I've encountered an uninterruptable code in Python 2.7.
from time import sleep
import threading
def fun():
for i in range(100):
print(i)
sleep(0.1)
if __name__ == '__main__':
threading.Timer(1, fun).start()
First, how come it is uninterruptable? (It ignores SIGINT)
In Python 3 this code interrupts fine.
Second, what changes should I make for it to respond to SIGINT before it finishes the loop?
In my actual case, it's an infinite loop :.(
I'm not yet sure why does it happen, but this phenomenon is mentioned in Python 3.2 bug fix report.
I found a workaround by unfolding the following code:
def fun():
while True:
... my code ...
sleep(0.1)
if __name__ == '__main__':
threading.Timer(1, fun).start()
Into:
def fun():
... my code ...
threading.Timer(0.1, fun).start()
if __name__ == '__main__':
threading.Timer(1, fun).start()
Related
def update():
while True:
loadData()
def main():
doStuff()
addToParallelFunctions(update)
doOtherStuff()
if __name__ == '__main__':
addToParallelFunctions(main)
How could that addToParallelFunctions function look like, so that update runs parallel to main and that I can add other functions to run also parallel to the main?
I've tried that, but it paused the main function and ran the other function until she was finished.
from multiprocessing import Process
def addProcess(*funcs):
for func in funcs:
Process(target=func).start()
I enhanced your example:
It works for me. Main script and function continue both.
import time
from multiprocessing import Process
def addProcess(*funcs):
for func in funcs:
Process(target=func).start()
def update():
for i in range(20):
print(i)
time.sleep(2)
def main():
print("start")
addProcess(update)
print("Main function continues")
if __name__ == '__main__':
addProcess(main)
print("main script continues")
Next time, please post an example that is reproducible.
I have the following code:
import time
def wait10seconds():
for i in range(10):
time.sleep(1)
return 'Counted to 10!'
print(wait10seconds())
print('test')
Now my question is how do you make print('test') run before the function wait10seconds() is executed without exchanging the 2 lines.
I want the output to be the following:
test
Counted to 10!
Anyone know how to fix this?
You can use Threads for this
like:
from threading import Thread
my_thread = Thread(target=wait10seconds) # Create a new thread that exec the function
my_thread.start() # start it
print('test') # print the test
my_thread.join() # wait for the function to end
You can use a Timer. Taken from the Python docs page:
def hello():
print("hello, world")
t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed
if you are using python 3.5+ you can use asyncio:
import asyncio
async def wait10seconds():
for i in range(10):
await asyncio.sleep(1)
return 'Counted to 10!'
print(asyncio.run(wait10seconds()))
asyncio.run is new to python 3.7, for python 3.5 and 3.6 you won't be able to use asyncio.run but you can achieve the same thing by working with the event_loop directly
The point of my script is to have one function called stopper send a terminate thread event to the function go_to, once it reaches a certain time.
Currently, the event is triggered, the thread supposedly closed, and stopper is ended. But go_to continues to print to the command line.
I can't figure out how to stop it correctly.
import threading
import time
class Stop_Check(object):
def __init__(self):
self.thread1Stop = threading.Event()
def stopper(self):
t = 0
while True:
t = t + 1
time.sleep(1.0)
if t == 3 :
self.thread1Stop.set()
break
else :
print("I'm still going...")
time.sleep(1.0)
continue
print("terminated")
def go_to(self):
while (not self.thread1Stop.is_set()):
print("I am working!")
time.sleep(1.0)
def main(self):
t1 = threading.Thread(target=self.go_to)
t1.start()
self.stopper()
time.sleep(5.0)
if __name__ == '__main__' :
sc = Stop_Check()
sc.main()
I tried running the script in the terminal, outside of the software called Atom. Now it terminates as expected. Thanks to #Steven for confirming it works! Still not sure why this behavior was happening.
I'm new to python and tornado. I was trying some stuff with coroutines.
def doStuff(callback):
def task():
callback("One Second Later")
Timer(1,task).start()
#gen.coroutine
def routine1():
ans = yield gen.Task(doStuff)
raise gen.Return(ans)
if __name__ == "__main__":
print routine1()
I'm trying to get the result of doStuff() function, which I expect to be "One Second Later". But it's not working. Any help would be appreciated. Thank you
What's probably happening is, you haven't started the IOLoop, nor are you waiting for your coroutine to complete before your script exits. You'll probably notice your script runs in a couple milliseconds, rather than pausing for a second as it ought. Do this:
if __name__ == "__main__":
from tornado.ioloop import IOLoop
print IOLoop.instance().run_sync(routine1)
I am using multi processing in Python. The following is the demo of my code:
In function main:
from multiprocessing import Process
def __name__ == "__main__":
print "Main program starts here."
SOME CODE....
process_1 = Process(target=proc1, args = (arg1, arg2))
process_2 = Process(target=proc2, args = (arg3, arg4))
process_1.start()
process_2.start()
process_1.join()
process_2.join()
And in function proc1 and proc2:
def proc1(arg1, arg2):
print "Proc1 starts from here."
SOME CODE....
So what I expect to see as output is:
Main program starts here.
Proc1 starts from here.
Proc2 starts from here.
However, what I got is:
Main program starts here.
Main program starts here.
Main program starts here.
It seems that both the proc1 and proc2 start the main rather than the procs.
May I know what is wrong with my code?
Many thanks.
def __name__ == "__main__":
Should be:
if __name__ == "__main__":
But if I execute your code with python 2.7 I get:
def __name__ == "__main__":
^
SyntaxError: invalid syntax
If I rename def to if:
Main program starts here.
proc2
proc1
So I wonder how you get your code working (you probably modified it before pasting it here)