guv.greenthread
- cooperative threads¶guv.greenthread.
GreenThread
(parent)[source]¶Bases: greenlet.greenlet
The GreenThread class is a type of Greenlet which has the additional property of being able
to retrieve the return value of the main function. Do not construct GreenThread objects
directly; call spawn()
to get one.
cancel
(*throw_args) → None[source]¶Kill the GreenThread using kill()
, but only if it hasn’t already started running
After being canceled, all calls to wait()
will raise throw_args (which default to
greenlet.GreenletExit
).
kill
(*throw_args) → None[source]¶Kill the GreenThread using kill()
After being killed all calls to wait()
will raise throw_args (which default to
greenlet.GreenletExit
).
link
(func, *curried_args, **curried_kwargs) → None[source]¶Set up a function to be called with the results of the GreenThread
The function must have the following signature:
func(gt, [curried args/kwargs])
When the GreenThread finishes its run, it calls func with itself and with the curried arguments supplied at link-time. If the function wants to retrieve the result of the GreenThread, it should call wait() on its first argument.
Note that func is called within execution context of the GreenThread, so it is possible to interfere with other linked functions by doing things like switching explicitly to another GreenThread.
unlink
(func, *curried_args, **curried_kwargs) → None[source]¶Remove linked function set by link()
Remove successfully return True, otherwise False
wait
() → None[source]¶Return the result of the main function of this GreenThread
If the result is a normal return value, wait()
returns it. If it raised an exception,
wait()
will raise the same exception (though the stack trace will unavoidably contain
some frames from within the GreenThread module).
guv.greenthread.
sleep
(seconds=0)[source]¶Yield control to the hub until at least seconds have elapsed
Parameters: | seconds (float) – time to sleep for |
---|
guv.greenthread.
spawn
(func, *args, **kwargs)[source]¶Spawn a GreenThread
Execution control returns immediately to the caller; the created GreenThread is scheduled to be run at the start of the next event loop iteration, after other scheduled greenlets, but before greenlets waiting for I/O events.
Returns: | GreenThread object which can be used to retrieve the return value of the function |
---|---|
Return type: | GreenThread |
guv.greenthread.
spawn_n
(func, *args, **kwargs)[source]¶Spawn a greenlet
Execution control returns immediately to the caller; the created greenlet is scheduled to be run at the start of the next event loop iteration, after other scheduled greenlets, but before greenlets waiting for I/O events.
This is faster than spawn()
, but it is not possible to retrieve the return value of
the greenlet, or whether it raised any exceptions. It is fastest if there are no keyword
arguments.
If an exception is raised in the function, a stack trace is printed; the print can be
disabled by calling guv.debug.hub_exceptions()
with False.
Returns: | greenlet object |
---|---|
Return type: | greenlet.greenlet |
guv.greenthread.
kill
(g, *throw_args)[source]¶Terminate the target greenlet/GreenThread by raising an exception into it
Whatever that GreenThread might be doing, be it waiting for I/O or another primitive, it sees an exception right away.
By default, this exception is GreenletExit, but a specific exception may be specified. throw_args should be the same as the arguments to raise; either an exception instance or an exc_info tuple.
Calling kill()
causes the calling greenlet to cooperatively yield.
Parameters: | g (greenlet.greenlet or GreenThread) – target greenlet/GreenThread to kill |
---|
guv.greenthread.
spawn_after
(seconds, func, *args, **kwargs)[source]¶Spawn a GreenThread after seconds have elapsed
Execution control returns immediately to the caller.
To cancel the spawn and prevent func from being called, call GreenThread.cancel()
on the
returned GreenThread. This will not abort the function if it’s already started running, which is
generally the desired behavior. If terminating func regardless of whether it’s started or not
is the desired behavior, call GreenThread.kill()
.
Returns: | GreenThread object which can be used to retrieve the return value of the function |
---|---|
Return type: | GreenThread |