[英]Eventlet and Python daemon, Foo not called?
我正在嘗試構建一個監聽隊列(Redis Kombu)的 Python 守護進程。 抓取任務並生成一個綠色線程來處理此任務。
我可以毫無問題地接收任務並使用它,但是當我嘗試使用 eventlet 生成 GreenThread 時,它似乎根本沒有做任何事情。
不打印,不顯示日志記錄。
class agent(Daemon):
"""
Agent
"""
def run(self):
# Setup connection
mainLogger.debug('Connecting to Redis')
connection = BrokerConnection(
hostname=agentConfig['redis_host'],
transport="redis",
virtual_host=agentConfig['redis_db'],
port=int(agentConfig['redis_port']))
connection.connect()
# Create an eventlet pool of size 5
pool = eventlet.GreenPool(5)
q = connection.SimpleQueue("myq")
while True:
try:
message = q.get(block=True, timeout=1)
print "GOT A MESSAGE FROM Q !"
pool.spawn_n(self.foo, 'x')
print "END SPAWN !"
except Empty:
mainLogger.debug('No tasks, going to sleep')
time.sleep(1)
def foo(self, x):
mainLogger.debug('\o/')
print "HELLO FROM SPAWN"
有什么我做錯了嗎?
我需要調用 eventlet.monkey_patch() 來調用 sleep() 來觸發上下文切換。
您只需要使用eventlet.sleep
,如下所述:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.