Last Updated: February 25, 2016
·
420
· hasenj

`go func()` in python

The excellent gevent has a method that can basically mimic go func() semantics in go.

Where in go you would write:

go func(arg1, arg2, arg3)

in python with gevent, you would write:

gevent.spawn(func, arg1, arg2, arg3)

This will spawn a greenlet and schedule it to be started. It will execute once the current greenlet yields.

If you’re running in a shell or a one-time script then you need to explicitly yield by calling gevent.sleep(0).

Note that gevent.spawn also accepts keyword arguments (which will be forwarded to the function).