Python tempfile模块和线程不是很好玩;我究竟做错了什么?

前端之家收集整理的这篇文章主要介绍了Python tempfile模块和线程不是很好玩;我究竟做错了什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在线程和 Python中的tempfile模块有一个有趣的问题.在线程退出之前,某些东西似乎没有得到清理,而且我正在针对打开的文件限制运行. (这是在OS X 10.5.8,Python 2.5.1上.)

然而,如果我复制tempfile模块正在做什么(不是所有的安全检查,而只是生成文件描述符然后使用os.fdopen来生成文件对象)我没有问题.

在将此作为Python的错误提交之前,我想我会在这里查看,因为我更有可能做了一些巧妙的错误.但是,如果我是,那一天试图解决它并没有让我到任何地方.

  1. #!/usr/bin/python
  2.  
  3. import threading
  4. import thread
  5. import tempfile
  6. import os
  7. import time
  8. import sys
  9.  
  10. NUM_THREADS = 10000
  11.  
  12. def worker_tempfile():
  13. tempfd,tempfn = tempfile.mkstemp()
  14. tempobj = os.fdopen(tempfd,'wb')
  15. tempobj.write('hello,world')
  16. tempobj.close()
  17. os.remove(tempfn)
  18. time.sleep(10)
  19.  
  20. def worker_notempfile(index):
  21. tempfn = str(index) + '.txt'
  22. # The values I'm passing os.open may be different than tempfile.mkstemp
  23. # uses,but it works this way as does using the open() function to create
  24. # a file object directly.
  25. tempfd = os.open(tempfn,os.O_EXCL | os.O_CREAT | os.O_TRUNC | os.O_RDWR)
  26. tempobj = os.fdopen(tempfd,world')
  27. tempobj.close()
  28. os.remove(tempfn)
  29. time.sleep(10)
  30.  
  31. def main():
  32. for count in range(NUM_THREADS):
  33. if count % 100 == 0:
  34. print('opening thread %s' % count)
  35. wthread = threading.Thread(target=worker_tempfile)
  36. #wthread = threading.Thread(target=worker_notempfile,args=(count,))
  37. started = False
  38. while not started:
  39. try:
  40. wthread.start()
  41. started = True
  42. except thread.error:
  43. print('Failed starting thread %s; sleeping' % count)
  44. time.sleep(3)
  45.  
  46. if __name__ == '__main__':
  47. main()

如果我在worker_notempfile行处于活动状态并且worker_tempfile行注释掉的情况下运行它,它将运行完成.

反过来(使用worker_tempfile)我收到以下错误

  1. $python threadtempfiletest.py
  2. opening thread 0
  3. opening thread 100
  4. opening thread 200
  5. opening thread 300
  6. Exception in thread Thread-301:
  7. Traceback (most recent call last):
  8. File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/threading.py",line 460,in __bootstrap
  9. File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/threading.py",line 440,in run
  10. File "threadtempfiletest.py",line 17,in worker_tempfile
  11. File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/tempfile.py",line 302,in mkstemp
  12. File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/tempfile.py",line 236,in _mkstemp_inner
  13. OSError: [Errno 24] Too many open files: '/var/folders/4L/4LtD6bCvEoipksvnAcJ2Ok+++Tk/-Tmp-/tmpJ6wjV0'

我有什么想法我做错了吗?这是Python中的一个错误,还是我头脑发热?

更新2009-12-14:
我想我找到了答案,但我不喜欢它.由于没有人能够复制这个问题,我去办公室寻找机器.除了我的机器,它传递了一切.我在Mac上使用与我使用的相同软件版本进行了测试.我甚至用我所拥有的完全相同的硬件和软件配置来寻找桌面G5 – 结果相同.两个测试(使用tempfile和没有tempfile)都成功了.

对于踢,我下载了Python 2.6.4,并在我的桌面上尝试了它,我的系统上的模式与Python 2.5.1相同:tempfile失败,并且notempfile成功.

这让我得出结论,我的Mac上出现了一些东西,但我肯定无法弄清楚是什么.欢迎任何建议.

解决方法

我无法在Mac OS X 10.5.9上重现(Apple自己构建的)Python 2.5.1的问题 – 运行完成就好了!

我已经尝试过Macbook Pro,即Intel处理器和旧的PowerMac,即PPC处理器.

所以我只能想象10.5.8中一定有一个我从未注意到的错误(没有任何10.5.8可以测试,因为我总是在软件更新提供的时候立即升级).我所能建议的是你尝试升级到10.5.9并查看错误是否消失 – 如果没有,我不知道我的机器和你的机器之间的这种行为差异是如何可能的.

猜你在找的Python相关文章