Highly concurrent networking library
—
Essential green threading primitives for managing cooperative concurrency in eventlet applications. These functions provide the foundation for creating, controlling, and managing greenthreads.
Create new greenthreads to run functions concurrently in a cooperative manner.
def spawn(func, *args, **kwargs):
"""
Create a greenthread to run func(*args, **kwargs).
Parameters:
- func: callable to run in new greenthread
- *args: positional arguments to pass to func
- **kwargs: keyword arguments to pass to func
Returns:
GreenThread instance that can be used to retrieve return value
"""
def spawn_n(func, *args, **kwargs):
"""
Create a greenthread to run func(*args, **kwargs) without return value access.
Same as spawn() but doesn't return a GreenThread object, making it slightly
more efficient when you don't need the return value.
Parameters:
- func: callable to run in new greenthread
- *args: positional arguments to pass to func
- **kwargs: keyword arguments to pass to func
Returns:
None
"""
def spawn_after(seconds, func, *args, **kwargs):
"""
Spawn a greenthread to run func(*args, **kwargs) after a delay.
Parameters:
- seconds: float, delay in seconds before spawning
- func: callable to run in new greenthread
- *args: positional arguments to pass to func
- **kwargs: keyword arguments to pass to func
Returns:
GreenThread instance
"""
def spawn_after_local(seconds, func, *args, **kwargs):
"""
Spawn a greenthread after a delay, but only if current greenthread is still alive.
Unlike spawn_after, the function will NOT be called if the current
greenthread has exited by the time the delay expires.
Parameters:
- seconds: float, delay in seconds before spawning
- func: callable to run in new greenthread
- *args: positional arguments to pass to func
- **kwargs: keyword arguments to pass to func
Returns:
GreenThread instance
"""Control the execution flow and lifecycle of greenthreads.
def sleep(seconds=0):
"""
Yield control to other greenthreads for the specified time.
Parameters:
- seconds: float, time to sleep in seconds (default: 0 for immediate yield)
Returns:
None
"""
def kill(gt, exc_type=None):
"""
Terminate a greenthread by raising an exception in it.
Parameters:
- gt: GreenThread instance to terminate
- exc_type: exception class to raise (default: greenlet.GreenletExit)
Returns:
None
"""
def getcurrent():
"""
Get the current greenthread.
Returns:
Current greenlet/greenthread instance
"""Schedule functions and exceptions to run after delays.
def exc_after(seconds, exc_type, *args, **kwargs):
"""
Schedule an exception to be raised in the current greenthread after delay.
Parameters:
- seconds: float, delay in seconds
- exc_type: exception class to raise
- *args: arguments to pass to exception constructor
- **kwargs: keyword arguments to pass to exception constructor
Returns:
Timer object that can be cancelled
"""
def call_after_global(seconds, func, *args, **kwargs):
"""
Schedule a function to be called after a delay.
Parameters:
- seconds: float, delay in seconds
- func: callable to run after delay
- *args: positional arguments to pass to func
- **kwargs: keyword arguments to pass to func
Returns:
Timer object that can be cancelled
"""
def spawn_after_local(seconds, func, *args, **kwargs):
"""
Spawn a greenthread after delay, cancelled if current greenthread exits.
Parameters:
- seconds: float, delay in seconds
- func: callable to run in new greenthread
- *args: positional arguments to pass to func
- **kwargs: keyword arguments to pass to func
Returns:
GreenThread instance
"""import eventlet
def worker(name, duration):
"""A simple worker function"""
print(f"Worker {name} starting")
eventlet.sleep(duration)
print(f"Worker {name} finished")
return f"Result from {name}"
# Spawn greenthreads and wait for results
gt1 = eventlet.spawn(worker, "A", 1.0)
gt2 = eventlet.spawn(worker, "B", 0.5)
# Spawn without return value access (more efficient)
eventlet.spawn_n(worker, "C", 2.0)
# Get results from greenthreads
result1 = gt1.wait() # Blocks until complete
result2 = gt2.wait()
print(f"Results: {result1}, {result2}")
# Delayed execution
eventlet.spawn_after(3.0, worker, "Delayed", 1.0)
# Schedule exception after delay
eventlet.exc_after(10.0, TimeoutError, "Operation timed out")
# Yield control to other greenthreads
eventlet.sleep(0) # Immediate yield
eventlet.sleep(1.0) # Sleep for 1 secondclass GreenThread(greenlet.greenlet):
"""
A greenlet subclass that can be used to retrieve return values and
provides additional lifecycle management methods.
"""
def wait(self):
"""
Wait for the greenthread to complete and return its result.
Returns:
The return value of the wrapped function
Raises:
Any exception raised by the wrapped function
"""
def kill(self, exc_type=None):
"""
Terminate this greenthread by raising an exception.
Parameters:
- exc_type: exception class to raise (default: greenlet.GreenletExit)
"""
def cancel(self, exc_type=None):
"""
Cancel this greenthread if it hasn't started running yet.
Parameters:
- exc_type: exception class to raise (default: greenlet.GreenletExit)
Note:
Has no effect if the greenthread has already started execution.
"""
def link(self, func, *curried_args, **curried_kwargs):
"""
Register a callback to be called when this greenthread exits.
Parameters:
- func: callable to invoke when greenthread exits
- *curried_args: additional args to pass to func
- **curried_kwargs: additional kwargs to pass to func
"""
def unlink(self, func, *curried_args, **curried_kwargs):
"""
Remove a previously registered callback.
Parameters:
- func: callable previously registered with link()
- *curried_args: args that were curried with the callback
- **curried_kwargs: kwargs that were curried with the callback
"""
def ready(self):
"""
Check if the greenthread has completed execution.
Returns:
bool: True if complete, False if still running
"""
def successful(self):
"""
Check if the greenthread completed successfully without exception.
Returns:
bool: True if successful, False if raised exception or still running
"""Install with Tessl CLI
npx tessl i tessl/pypi-eventlet