- 论坛徽章:
- 0
|
谢谢 jeppeter大牛帮助
我现在改成了下面这个版本,它似乎能立即回收了。
- import os,sys,time,multiprocessing,queue,threading,signal
- pdict = {}
- count = 0
- def child(name,req):
- while True:
- ret = 'a'
- try:
- ret = req.get(timeout=5)
- print("%s get %s" %(name,ret))
- time.sleep(2)
- msg = "%s exit now" %(name)
- print(msg)
- time.sleep(1)
- req.close()
- del req
- os._exit(4)
- except Exception as e:
- print("child err,%s" %str(e))
- def start_child(name,req):
- print("to start %s now" %name)
- p = multiprocessing.Process(target=child, args=(name,req) )
- p.start()
- pdict[name] = {}
- pdict[name]['queue'] = [req,p]
- pdict[name]['other'] = 'otherinfo'
- def main():
- print(" main start now")
- req = ''
-
- #signal.signal(signal.SIGCHLD,signal.SIG_IGN)
- while True:
- count = 0
- while True:
- count = count + 1
- name = "child#%s" %count
- req = multiprocessing.Queue()
-
- print("\n")
- print("start %s child now ......" %name)
- p1 = threading.Thread( target=start_child,args = (name,req))
- p1.start()
- req.put([count])
- time.sleep(4)
- req = pdict[name]['queue'][0]
- p = pdict[name]['queue'][1]
- p.join()
- req.close()
- del req
- del p
- print("clear %s resource\n" %name)
- pdict[name].pop('queue',None)
- req = None
- p = None
- time.sleep(5)
- print(" begin the %s round now #################" %(count+1))
- if __name__ == "__main__":
- main()
复制代码 |
|