Firstly the following code ideally should run the 3 greenlets synchronously, but instead it runs all 3 greenlets asynchronously. However the strange thing happening is it starts an additional synchronous process for the second greenlet no matter how many greenlets you have. I am not asking this question for an workaround, its just that I want to understand the reason behind this.
import gevent
import time
def func(i):
t = time.time()
print('func %s started at %s' % (i, t))
secs = 5
statement = "after %s secs" % secs
gevent.sleep(secs)
print('func %s stopped in %s secs' % (i, (time.time() - t)))
apps = [gevent.Greenlet.spawn(func, i) for i in range(3)]
[app.run() for app in apps]
Here's the sample stdout:
func 0 started at 1491859273.2895772
func 1 started at 1491859273.2898045
func 2 started at 1491859273.2899446
func 0 stopped in 5.0095603466033936 secs
func 1 started at 1491859278.2993205
func 1 stopped in 5.0163233280181885 secs
func 2 stopped in 5.019707918167114 secs
func 1 stopped in 5.009198188781738 secs
How is func 1 started
happening twice?