greenio facilitates the integration of synchronous
third-party libraries into pulsar asynchronous framework.
It requires the greenlet library.
If you want to understand how integration works but you are unfamiliar with greenlets, check out the greenlet documentation first.
This application does not use monkey patching and therefore it works quite differently from implicit asynchronous libraries such as gevent. This module provides the user with a set of utilities for explicitly transferring execution from one greenlet to another which executes the blocking call in a greenlet-friendly way.
The caller has the responsibility that the blocking call is greenlet-friendly, i.e. it transfers the control of execution back to the parent greenlet when needed.
Lets assume you are building an application which uses pulsar asynchronous engine and would like to
- either use an external library written in blocking style, i.e. without yielding control to the event loop when IO calls are performed.
- or write your client code without dealing with
Futureor coroutines, in other words in an implicit asynchronous style. In this way your client code can be used on other frameworks just as well.
In both cases, the
greenio application is what you need.
Assume you are using pulsar web server and would like to write your application
in an implicit asynchronous mode, i.e. without dealing with futures nor
coroutines, then you can wrap your WSGI
app with the
from pulsar.apps.wsgi import WSGIServer from pulsar.apps.greenio import GreenPool, GreenWSGI green_pool = greenio.GreenPool() callable = GreenWSGI(app, green_pool) WSGIServer(callable=callable).start()
HttpClient can be used with greenlets:
>>> from pulsar.apps.greenio import GreenHttp >>> http = GreenHttp()
And now you can write synchronous looking code and run it in a separate
greenlet via the
@greenio.run_in_greenlet def example(): response = http.get('http://bbc.co.uk') ... return response.text()
and somewhere, in your asynchronous code:
result = await example() result == ...
run_in_greenlet() decorator, execute the function on a child
greenlet without blocking the asynchronous engine. Once the
function returns, the asynchronous code continue from the
statement as usual.
Wait for a possible asynchronous value to complete.
Run in greenlet¶
Decorator to run a
callableon a new greenlet.
callabledecorated with this decorator returns a coroutine
A pool of running greenlets.
This pool maintains a group of greenlets to perform asynchronous tasks via the
True if this pool is closed and no task can queued
True if the current greenlet is a green pool worker
A Locking primitive that is owned by a particular greenlet when locked.The main greenlet cannot acquire the lock.
A primitive lock is in one of two states, ‘locked’ or ‘unlocked’.
It is created in the unlocked state. It has two basic methods,
release(). When the state is unlocked,
acquire()changes the state to locked and returns immediately.
When the state is locked,
acquire()blocks the current greenlet until a call to
release()changes it to unlocked, then the
acquire()call resets it to locked and returns.
‘Return the greenlet that acquire the lock or None.
Acquires the lock if in the unlocked state otherwise switch back to the parent coroutine.
Release the lock.
This method should only be called in the locked state; it changes the state to unlocked and returns immediately. If an attempt is made to release an unlocked lock, a RuntimeError will be raised.