Skip to content

Instantly share code, notes, and snippets.

@neuroid
Created June 6, 2014 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neuroid/5d42760310f7af843150 to your computer and use it in GitHub Desktop.
Save neuroid/5d42760310f7af843150 to your computer and use it in GitHub Desktop.
gevent.subprocess vs os.waitpid
from gevent import monkey
monkey.patch_subprocess()
import os
import signal
import subprocess
import gevent
def task():
while True:
# print('task')
subprocess.call(['echo', 'task'])
gevent.sleep(1)
if __name__ == '__main__':
pid = gevent.fork()
if pid == 0:
running = True
def _stop():
global running
running = False
gevent.signal(signal.SIGTERM, _stop)
gevent.signal(signal.SIGINT, _stop)
while running:
print('process')
gevent.sleep(1)
os._exit(0)
greenlet = gevent.spawn(task)
def _stop():
os.kill(pid, signal.SIGTERM)
greenlet.kill()
gevent.signal(signal.SIGTERM, _stop)
gevent.signal(signal.SIGINT, _stop)
while True:
print('waiting for process')
pid, status = os.waitpid(-1, os.WNOHANG)
if pid:
print('process {} exited with status {}'.format(pid, status))
break
gevent.sleep(0.1)
greenlet.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment