Spawning greenlets via gevent

Gevent provides a wrapper Greenlet class around base greenlet library. Reference

In our own source code, we will therefore depend on

import gevent
from gevent import Greenlet

to implement our custom logic.

  1. Via the base Greenlet class:

    import gevent
    from gevent import Greenlet
    def foo(message, n):
        Each thread will be passed the message, and n arguments
        in its initialization.
    # Initialize a new Greenlet instance running the named function
    # foo
    thread1 = Greenlet.spawn(foo, "Hello", 1)
    # Wrapper for creating and runing a new Greenlet from the named
    # function foo, with the passed arguments
    thread2 = gevent.spawn(foo, "I live!", 2)
    # Lambda expressions
    thread3 = gevent.spawn(lambda x: (x+1), 2)
    threads = [thread1, thread2, thread3]
    # Block until all threads complete.
  2. Subclassing the base Greenlet class and using internal method _run

import gevent
from gevent import Greenlet

class MyGreenlet(Greenlet):

    def __init__(self, message, n):
        self.message = message
        self.n = n

    def _run(self):

g = MyGreenlet("Hi there!", 3)

Full Tutorial on gevent

See for all the detailed explanations of gevent functionalities.