I am new to async programming. I am doing a small POC where I want to see how greenlets
behave with shared objects. I have written this code -
from gevent import monkey, sleep
from gevent import Greenlet
monkey.patch_all(thread=False, socket=False)
class Events:
def __init__(self):
self.num = 0
self.limit = 10
self.bulk_records = []
def start(self, task_number=0):
count = 0
print("Task: %s started" % task_number)
while count != 10:
self.bulk_records.append(task_number)
sleep(0.1)
if len(self.bulk_records) == self.limit:
print("%s, Task %s, outputting list: %s" % (self.num, task_number, self.bulk_records))
self.bulk_records[:] = []
self.num += 1
count += 1
print("Task - %s, count - %s" % (task_number, count))
def run_test():
event = Events()
tasks = [Greenlet.spawn(event.start, i) for i in range(5)]
print("Num tasks - %s" % len(tasks))
[task.run() for task in tasks]
print("End")
if __name__ == '__main__':
run_test()
It gives the following output:-
Num tasks - 5
Task: 0 started
Task: 1 started
Task: 2 started
Task: 3 started
Task: 4 started
0, Task 0, outputting list: [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
1, Task 0, outputting list: [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
2, Task 0, outputting list: [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
3, Task 0, outputting list: [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
4, Task 0, outputting list: [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
Task - 0, count - 10
Task: 1 started # This is not expected
Task - 1, count - 10
Task - 2, count - 10
Task - 3, count - 10
Task - 4, count - 10
5, Task 1, outputting list: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Task - 1, count - 10
End
Process finished with exit code 0
The code works as expected but in the end something weird happened, Task 1 is started again and the corresponding function start
is executed again.
Not sure if this is a valid behavior, or what I am missing in the code to prevent Greenlet
to start again.
Thanks is advance
Contrary to the thread API, which requires a thread object to be created and to call the start()
method, gevent.spawn
does both greenlet instantiation and schedules the greenlet to run in one call.
So, calling run()
on greenlet objects is not needed (and probably very wrong in most cases).
Waiting for tasks to be completed is done with joinall(list of greenlets)
. After this call, all greenlets are ready (dead, successfully or not).