Gevent task with endless loop seems to block every other task aswell

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-06 03:16:10

问题


I'm quite new to Gevent/Greenlet and have gone through the tutorials. I would like to run a bot for each registered team.

for bot in self.bots:
    events.append(gevent.spawn(bot.start))
gevent.joinall(events)

The interesting part is that if I don't use a while true loop, I get the bot_id of both bots shown in the console.

def start(self):
        while True:
            for reply in self.slack_client.rtm_read():
                self.input(reply)
            time.sleep(0.1)
            logger.info("Log:{0}".format(self.bot_id))

But as soon as I use a endless loop, I can only see one bot's id being displayed. It seems as if the other task is waiting for this one to finish, which makes no sense. I thought that gevent.joinall would run both in parallel.

any advice on this please?

UPDATE

For the record, I had to add gevent.sleep(0.1) on the last line of while loop to make this work.


回答1:


From the Gevent introduction:

Only one greenlet is ever running at any given time.

Basically I think that what you are looking for is parallelism not asynchronous operations. Maybe a better fit would be to use the multiprocessing module.



来源:https://stackoverflow.com/questions/37034016/gevent-task-with-endless-loop-seems-to-block-every-other-task-aswell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!