Eventlet 和 Python 守护进程,Foo 没有被调用?
我正在尝试构建一个监听队列(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"
我做错了什么吗?
I am trying to build a Python deamon which listen to a queue (Redis Kombu).
Grab the task and spawn a greenthread to process this task.
I can receive the task and consume it without trouble but when I try to spawn a GreenThread with eventlet it does not seem to be doing anything at all.
No print, no logging is shown.
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"
Anything I am doing wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我需要调用 eventlet.monkey_patch() 进行 sleep() 调用来触发上下文切换。
I needed to call eventlet.monkey_patch() for sleep() call to trigger context switching.
您只需要按照此处所述使用
eventlet.sleep
:http: //eventlet.net/doc/basic_usage.html#eventlet.sleep
You just need to use
eventlet.sleep
as described here:http://eventlet.net/doc/basic_usage.html#eventlet.sleep