python线程异常中断,python的线程没有“中断”?

As far as I know python''s threading module models after Java''s.

However, I can''t find something equivalent to Java''s interrupt and

isInterrupted methods, along with InterruptedException.

"somethread.interrupt()" will wake somethread up when it''s in

sleeping/waiting state.

Is there any way of doing this with python''s thread? I suppose thread

interrupt is a very primitive functionality for stopping a blocked

thread.

Jane

解决方案

ja***********@hotmail.com (Jane Austine) wrote in message news:...As far as I know python''s threading module models after Java''s.

However, I can''t find something equivalent to Java''s interrupt and

isInterrupted methods, along with InterruptedException.

"somethread.interrupt()" will wake somethread up when it''s in

sleeping/waiting state.

Is there any way of doing this with python''s thread? I suppose thread

interrupt is a very primitive functionality for stopping a blocked

thread.

Jane

Well, I haven''t got any answer since I posted it. Meanwhile, I have

been searching for it myself. Something new has been added in 2.3 in

thread module. That''s interrupt_main. But, unfortunately, it is the

opposite of what I expected; It interrupts the main thread.

After all this, I am a bit disappointed about Python. (it''s sad)

Jane

>As far as I know python''s threading module models after Java''s.

However, I can''t find something equivalent to Java''s interrupt and

isInterrupted methods, along with InterruptedException.

"somethread.interrupt()" will wake somethread up when it''s in

sleeping/waiting state.

Is there any way of doing this with python''s thread? I suppose thread

interrupt is a very primitive functionality for stopping a blocked

thread.

Well, I haven''t got any answer since I posted it. Meanwhile, I have

been searching for it myself. Something new has been added in 2.3 in

thread module. That''s interrupt_main. But, unfortunately, it is the

opposite of what I expected; It interrupts the main thread.

After all this, I am a bit disappointed about Python. (it''s sad)

Did you try condition objects? threading.Condition

Example from the library reference:

# Consume one item

cv.acquire()

while not an_item_is_available():

cv.wait()

get_an_available_item()

cv.release()

# Produce one item

cv.acquire()

make_an_item_available()

cv.notify()

cv.release()

You can block a thread with a condition object by calling its ''wait()''

method. You can

call the ''notify()'' method from another thread and that will ''interrupt''

the blocked thread.

It is not the very same thing, but I suspect you can use it for the same

purposes.

Cheers,

Laci 1.0

> >As far as I know python''s threading module models after Java''s.

However, I can''t find something equivalent to Java''s interrupt and

isInterrupted methods, along with InterruptedException.

"somethread.interrupt()" will wake somethread up when it''s in

sleeping/waiting state.

Is there any way of doing this with python''s thread? I suppose thread

interrupt is a very primitive functionality for stopping a blocked

thread.

Well, I haven''t got any answer since I posted it. Meanwhile, I have

been searching for it myself. Something new has been added in 2.3 in

thread module. That''s interrupt_main. But, unfortunately, it is the

opposite of what I expected; It interrupts the main thread.

After all this, I am a bit disappointed about Python. (it''s sad)

Did you try condition objects? threading.Condition

Example from the library reference:

# Consume one item

cv.acquire()

while not an_item_is_available():

cv.wait()

get_an_available_item()

cv.release()

# Produce one item

cv.acquire()

make_an_item_available()

cv.notify()

cv.release()

You can block a thread with a condition object by calling its ''wait()''

method. You can

call the ''notify()'' method from another thread and that will ''interrupt''

the blocked thread.

It is not the very same thing, but I suspect you can use it for the same

purposes.

Cheers,

Laci 1.0

Thanks, but it doesn''t give a solution. The problem is that there can

be multiple condition variables and I have to interrupt the thread no

matter what condition variable is waiting.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值