Threading API¶
The threading API is used by the middleware to provide an easy interface to create and use threads. Threads provided by this API are implemented using the EventQueue object which provides a easy way of serializing operations and running them in another thread. The API also provides decorators and functions to easily run functions in the desired thread.
Thread Manager¶
The Thread Manager is a Singleton
responsible for creating and keeping track of EventQueue based threads. It's used by the configuration framework in order to run all configuration saving/deleting operations in a single thread, for example.
methods¶
__init__(self)
Instantiates a new ThreadManager or returns the already instantiated ThreadManager instance, since it's a
Singleton
.
start(self)
Start the ThreadManager. This method doesn't really do anything since threads are created by using the
get_thread
function.
stop(self)
Stop the ThreadManager. All threads will be stopped and joined.
get_thread(self, thread_id)
Return the thread matching the given ID. If the ID doesn't exist it will be created and added to the ThreadManager's internal data.
stop_thread(self, thread_id)
Stops the thread matching the given ID. The thread will also be joined from a internal thread whose ID is thread-ops.
utility functions and decorators¶
call_in_thread(thread_id, func, *args, **kwargs)
Function which will run the given func in the specified thread.
run_in_thread(thread_id)
Decorator which will run the decorated function in the specified thread.
The reactor thread and green threads¶
SIP SIMPLE SDK uses Twisted's reactor to run an event loop where some internal operations are performed.
Green threads are threads that work in userspace on top of one (or more) native threads in a cooperative way. They are not preemptive, so only one green thread may run at a given moment and another one can only run if the first yielded the control explicitly (unlike preemptive threads). Green threads run on top of the reactor thread.
utility functions and decorators¶
Utility functions and decorators are provided in order to run code in the reactor thread or in a new green thread in the sipsimple.threading
and sipsimple.threading.green
packages.
call_in_twisted_thread(func, *args, **kwargs)
Function which will run the given func in the reactor thread.
run_in_twisted_thread
Decorator which will run the decorated function in the reactor thread.
call_in_green_thread(func, *args, **kwargs)
Function which will spawn a new green thread and run the given func there.
run_in_green_thread
Decorator which will spawn a new green thread and run the decorated function there.
run_in_waitable_green_thread
Decorator which will spawn a new green thread and run the decorated function there with the ability of waiting for the result of the function. This will make code look blocking, but as it's running in a green thread it's not, since it's cooperating with other green threads.