Created
June 6, 2014 12:36
gevent.subprocess vs os.waitpid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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