python线程自动释放_如何在执行run()之后自动释放线程中的信号量?

本文探讨了如何在Python线程中使用信号量,并期望在`run()`方法执行完毕后自动释放。目前的实现导致了线程死锁,因为信号量在`start()`方法中获取并在`join()`时释放。理想情况下,希望在线程执行结束时自动释放信号量。然而,尝试重载`threading.Thread`以在目标函数完成后释放信号量并未达到预期效果。寻求解决方案。
摘要由CSDN通过智能技术生成

我用这种方式跟踪信号量的线程数:#!/usr/bin/python3

import threading

class MyThread(threading.Thread):

max_threads = 5

max_threads_semaphore = threading.BoundedSemaphore(value=max_threads)

semaphore_timeout = 60

def __init__(self, target=None, name=None, args=(), kwargs={}):

super().__init__(target=target, name=name, args=args, kwargs=kwargs)

def start(self):

semaphore_aquired = self.max_threads_semaphore.acquire(blocking=True, timeout=self.semaphore_timeout)

if semaphore_aquired:

print("Sempahore acquired by:", self.name)

super().start()

else:

raise OSError("Time out aquiring max threads semaphore to start new thread")

def join(self):

super().join()

semaphore_released = self.max_threads_semaphore.release()

这是工作,但不是我想要的方式。我希望能够:

^{pr2}$

显然,一旦启动了5个线程(max_threads),脚本就会处于死锁状态,直到信号量超时为止。在

我认为start()方法将在一个新线程中启动run()方法。来自the manual:start()

It must be called at most once per thread object. It arranges

for the object’s run() method to be invoked in a separate thread of

control.

所以在MyThread中,我将join()部分替换为:def run(self):

super().run()

semaphore_released = self.max_threads_semaphore.release()

print("Sempahore released by:", self.name)

但这段代码不会在新线程中执行。输出为:executing: 1

end of execution: 1

Sempahore acquired by: Thread-1

Sempahore released by: Thread-1

executing: 2

end of execution: 2

Sempahore acquired by: Thread-2

Sempahore released by: Thread-2

executing: 3

end of execution: 3

Sempahore acquired by: Thread-3

Sempahore released by: Thread-3

executing: 4

end of execution: 4

Sempahore acquired by: Thread-4

Sempahore released by: Thread-4

executing: 5

end of execution: 5

Sempahore acquired by: Thread-5

Sempahore released by: Thread-5

executing: 6

end of execution: 6

Sempahore acquired by: Thread-6

executing: 7

Sempahore released by: Thread-6

end of execution: 7

Sempahore acquired by: Thread-7

executing: 8

Sempahore released by: Thread-7

end of execution: 8

Sempahore acquired by: Thread-8

executing: 9

Sempahore released by: Thread-8

end of execution: 9

Sempahore acquired by: Thread-9

joined: Thread-1

joined: Thread-2

joined: Thread-3

joined: Thread-4

joined: Thread-5

joined: Thread-6

joined: Thread-7

joined: Thread-8

Sempahore released by: Thread-9

joined: Thread-9

有没有办法重载threading.Thread函数,以便在目标函数结束时自动释放信号量?在

我不明白为什么输出不是:Sempahore acquired by: Thread-1

executing: 1

Sempahore acquired by: Thread-2

executing: 2

Sempahore acquired by: Thread-3

executing: 3

Sempahore acquired by: Thread-4

executing: 4

Sempahore acquired by: Thread-5

executing: 5

(sleep 10 secondes)

end of execution: 1

Sempahore released by: Thread-1

Sempahore acquired by: Thread-6

executing: 6

etc..

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值