You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm looking for the best way to execute some finalization in my greenlets upon SIGINT, SIGTERM, ...
It seems that gevent.signal allows me to register signal handlers but they are excuted directly in the hub so I suppose I can't do gevent stuff (like killing a greenlet and waiting until killed).
In the example below I'm using hub watchers instead. My questions:
Is this the best way to making sure greenlet finalization is called when the greenlet is killed or upon SIGINT, SIGTERM, ...
It seems that I need to call os.kill in the watcher handler because, for example, sometimes I need a second SIGINT to close the process
Example (run and press CTRL-C before it ends):
importosfromcontextlibimportcontextmanagerimportgeventfromgeventimportmonkeyfromgeventimportsignalfromgeventimporthubmonkey.patch_all(thread=False)
defregister_signal_handler(signalnum, handler, *args, **kw):
defwrapper():
try:
handler(*args, **kw)
finally:
watcher.cancel()
os.kill(os.getpid(), signalnum)
watcher=hub.signal(signalnum, wrapper)
returnwatcher@contextmanagerdefkill_on_exit(timeout=3):
handler=gevent.getcurrent().killwatchers= [
register_signal_handler(signalnum, handler, timeout=timeout)
forsignalnumin (signal.SIGQUIT, signal.SIGINT, signal.SIGTERM)
]
try:
yieldfinally:
forwinwatchers:
try:
w.cancel()
exceptExceptionase:
print(f"Exception during kill_on_exit cleanup: {e}")
defmain(filename):
# This is just a random example to illustrate proper finalizationprint("Start writing to ", filename)
withopen(filename, mode="w") asf:
try:
foriinrange(100):
print(f"Writing {i} to ", filename)
print(i, file=f)
gevent.sleep(1)
finally:
print("Finalize ", filename)
f.write("END")
defsafe_main(filename):
withkill_on_exit():
main(filename)
glts= [gevent.spawn(safe_main, f"/tmp/file{i}.txt") foriinrange(10)]
gevent.joinall(glts)
The text was updated successfully, but these errors were encountered:
gevent makes the same basic guarantee that Python does with regards to signals: They are delivered to the main greenlet (or main thread). Other than that, how you handle them will be application specific, depending on exactly what you're trying to do and why.
I can't say I've had a need to do anything like what you're showing here.
I'm looking for the best way to execute some finalization in my greenlets upon SIGINT, SIGTERM, ...
It seems that
gevent.signal
allows me to register signal handlers but they are excuted directly in the hub so I suppose I can't do gevent stuff (like killing a greenlet and waiting until killed).In the example below I'm using hub watchers instead. My questions:
os.kill
in the watcher handler because, for example, sometimes I need a second SIGINT to close the processExample (run and press CTRL-C before it ends):
The text was updated successfully, but these errors were encountered: