Asynchronous Helpers

class support.async.CPUThread[source]

Manages a single worker thread to dispatch cpu intensive tasks to.

Signficantly less overhead than gevent.threadpool.ThreadPool() since it uses prompt notifications rather than polling. The trade-off is that only one thread can be managed this way.

Since there is only one thread, hub.loop.async() objects may be used instead of polling to handle inter-thread communication.

apply(func, args, kwargs)[source]
class support.async.GreenConsole(locals=None, filename='<console>')[source]
raw_input(*a, **kw)[source]
class support.async.Greenlet(f, *a, **kw)[source]

A subclass of gevent.Greenlet which adds additional members:

  • locals: a dict of variables that are local to the “spawn tree” of greenlets
  • spawner: a weak-reference back to the spawner of the greenlet
  • stacks: a record of the stack at which the greenlet was spawned, and ancestors
class support.async.ThreadPoolDispatcher(pool, name)[source]
support.async.check_fork(fn)[source]

Hack for Django/gevent interaction to reset after non-gevent fork

support.async.close_threadpool()[source]
support.async.cpu_bound(f, p=None)[source]

Cause the decorated function to have its execution deferred to a separate thread to avoid blocking the IO loop in the main thread. Useful for wrapping encryption or serialization tasks.

Example usage:

@async.cpu_bound def my_slow_function():

pass
support.async.cpu_bound_if(p)[source]

Similar to cpu_bound, but should be called with a predicate parameter which determines whether or not to dispatch to a cpu_bound thread. The predicate will be passed the same parameters as the function itself.

Example usage:

# will be deferred to a thread if parameter greater than 16k, # else run inline @async.cpu_bound_if(lambda s: len(s) > 16 * 1024) def my_string_function(data):

pass
support.async.get_spawntree_local(name)[source]

Essentially provides dynamic scope lookup for programming aspects that cross greenlet borders. Be wary of overusing this functionality, as it effectively constitutes mutating global state which can lead to race conditions and architectural problems.

support.async.greenify(banner='REPL is now greenlet friendly (exit with Ctrl+C)')[source]
support.async.in_threadpool()[source]

function to return the threadpool in which code is currently executing (if any)

support.async.killsock(sock)[source]

Attempts to cleanly shutdown a socket. Regardless of cleanliness, ensures that upon return, the socket is fully closed, catching any exceptions along the way. A safe and prompt way to dispose of the socket, freeing system resources.

support.async.set_spawntree_local(name, val)[source]

Similar to get_spawntree_local except that it allows setting these values. Again, be wary of overuse.

support.async.staggered_retries(run, *a, **kw)[source]

A version of spawn that will block will it is done running the function, and which will call the function repeatedly as time progresses through the timeouts list.

Best used for idempotent network calls (e.g. HTTP GETs).

e.g. user_data = async.staggered_retries(get_data, max_results,

latent_data_ok, public_credential_load, timeouts_secs=[0.1, 0.5, 1, 2])

returns None on timeout.

support.async.start_repl(local=None, banner='infra REPL (exit with Ctrl+C)')[source]
support.async.timed(f)[source]

wrap a function and time all of its execution calls in milliseconds