2

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.

Houman
  • 64,245
  • 87
  • 278
  • 460

1 Answers1

3

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.

lesingerouge
  • 1,160
  • 7
  • 14
  • Thanks, in fact I found out that `joinall()` is concurrency ran at the same time. Which is not parallelism. Hence I added `gevent..sleep(0.1)` at the end of the loop and now it's working just fine. What is your opinion, would that scale for this purpose? Because multiprocessing takes a lot of resources (memory-wise) – Houman May 05 '16 at 09:56
  • @Houman it should work pretty well if that input function is simple enough. But you should test the limit for the number of bots per machine. – lesingerouge May 05 '16 at 10:04