用id杀死Python Greenlet

时间:2016-09-14 23:47:36

标签: python greenlets

Greenlet.spawn创建了一个新的Greenlet实例,如docs中所定义。我有下面的代码,我希望能够通过它的id杀死Greenlet。

import gevent
from gevent import Greenlet
from gevent import getcurrent

class BidTask(Greenlet):
    """ A new bid task creates a new thread when initialized """
    def __init__(self, bid):
        Greenlet.__init__(self)
        self.bid = bid

    def task(self):
      while True:
        print("Running %r" % [self.bid, id(getcurrent())])
        gevent.sleep(10) # sleep for 10 secs
        print("Running %r" % [self.bid, id(getcurrent())])

    def _run(self):
        self.task()

g = BidTask("Bid 1")
g.spawn("Bid 1") # a new instance of Greenlet is created

g2 = BidTask("Bid 2")
g2.spawn("Bid 2") # a new instance of Greenlet is created

gevent.joinall([g, g2])


# Elsewhere in another module
class Outsider(object):
  def kill(self, id):
    Greenlet.registry(id).kill() 

Outsider().kill(1234) # How to achieve this?

感谢您的帮助。

0 个答案:

没有答案