Skip to content

Instantly share code, notes, and snippets.

@youngsofun
Created March 16, 2018 03:06
Show Gist options
  • Save youngsofun/4fa0ee2114498f8ea23f724d2f3239e9 to your computer and use it in GitHub Desktop.
Save youngsofun/4fa0ee2114498f8ea23f724d2f3239e9 to your computer and use it in GitHub Desktop.
import multiprocessing
import threading
import time
import os
import psutil
import sys
import errno
def worker():
print('warker start')
time.sleep(10)
print('worker exit')
os._exit(3)
#sys.exit(3)
#return 3
def check_mem():
time.sleep(5)
print('check_mem end')
os._exit(3)
def worker2():
print('warker start')
t = threading.Thread(target=check_mem)
t.daemon = True
t.start()
while True:
time.sleep(10)
print('worker exit')
def executor():
proc = multiprocessing.Process(target=worker2)
proc.daemon = True
proc.start()
pid = proc.pid
print('work pid', pid)
ps = psutil.Process(pid)
while not (ps.status() == psutil.STATUS_ZOMBIE or not ps.is_running()):
print('working, ', ps.status())
time.sleep(1)
proc.join(1)
print('exit code', proc.exitcode)
try:
os.waitpid(pid, os.WNOHANG)
except OSError as e:
if e.errno == errno.ECHILD:
print("errno.ECHILD")
else:
logger.exception('process termination fail: ', e.message)
executor()
@youngsofun
Copy link
Author

youngsofun commented Mar 16, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment