python线程不死_在python中,线程何时死亡?

查看您的previous question(您在评论中链接了),问题是文件描述符用完了.

File descriptors are small integers corresponding to a file that has been opened by the current process. For example, standard input is usually file descriptor 0, standard output is 1, and standard error is 2. Further files opened by a process will then be assigned 3, 4, 5, and so forth. The name “file descriptor” is slightly deceptive; on Unix platforms, sockets and pipes are also referenced by file descriptors.

现在我正在猜测,但可能是您正在执行以下操作:

class Wroker(threading.Thread):

def __init__(self):

threading.Thread.__init__(self)

def run(self):

my_file = open('example.txt')

# operations

my_file.close() # without this line!

您需要关闭文件!

您可能正在启动多个线程,并且每个线程都在打开但没有关闭文件,这样一段时间后,您就没有更多的“小整数”要分配给打开新文件了.

还要注意,在#operations部分中可能发生任何事情,如果引发异常,除非包裹在try / finally语句中,否则文件不会关闭.

有一种更好的文件处理方式:with statement:

with open('example.txt') as my_file:

# bunch of operations with the file

# other operations for which you don't need the file

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值