免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: qqrilxk
打印 上一主题 下一主题

python中文件句柄的关闭问题 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2016-09-28 10:08 |只看该作者
谢谢 jeppeter大牛帮助
我现在改成了下面这个版本,它似乎能立即回收了。
  1. import os,sys,time,multiprocessing,queue,threading,signal
  2. pdict = {}
  3. count = 0


  4. def child(name,req):
  5.         while True:
  6.                 ret = 'a'
  7.                 try:
  8.                         ret = req.get(timeout=5)
  9.                         print("%s get %s" %(name,ret))
  10.                         time.sleep(2)
  11.                         msg = "%s  exit now" %(name)
  12.                         print(msg)
  13.                         time.sleep(1)
  14.                         req.close()
  15.                         del req
  16.                         os._exit(4)
  17.                 except Exception as e:
  18.                         print("child err,%s" %str(e))

  19. def start_child(name,req):
  20.         print("to start %s now" %name)
  21.         p = multiprocessing.Process(target=child, args=(name,req) )
  22.         p.start()
  23.         pdict[name] = {}
  24.         pdict[name]['queue'] = [req,p]
  25.         pdict[name]['other'] = 'otherinfo'


  26. def main():
  27.         print(" main start now")
  28.         req = ''
  29.                
  30.         #signal.signal(signal.SIGCHLD,signal.SIG_IGN)

  31.         while True:
  32.                 count = 0
  33.                 while True:
  34.                         count = count + 1
  35.                         name = "child#%s" %count
  36.                         req = multiprocessing.Queue()
  37.                
  38.                         print("\n")
  39.                         print("start %s child now ......" %name)
  40.                         p1 = threading.Thread( target=start_child,args = (name,req))                                                                                                                       
  41.                         p1.start()

  42.                         req.put([count])                       
  43.                         time.sleep(4)                       

  44.                         req = pdict[name]['queue'][0]
  45.                         p = pdict[name]['queue'][1]
  46.                         p.join()
  47.                         req.close()
  48.                         del req
  49.                         del p
  50.                         print("clear %s resource\n" %name)
  51.                         pdict[name].pop('queue',None)
  52.                         req = None
  53.                         p = None
  54.                         time.sleep(5)
  55.                         print(" begin the %s round now #################" %(count+1))                                       

  56. if __name__ == "__main__":
  57.         main()
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP