Package: threadswitch
PyThreadSwitchPython microlibrary to run arbitrary tasks in contexts associated with a single thread.CAVEAT:This library is currently not well tested and requires unit tests for many more different scenarios. Any
contribution would be greatly appreciated.InstallationSimply install usingpip install threadswitch.UsageThreadSwitch currently implements two kinds of thread contexts:PersistentContextA context associated with a background thread which must be manually closed. This context is best suited for system
resources which are restricted to the same thread, such as OpenGL and SQLite3 objects (in a standard CPython installation).
While it providesstart,stop, andresetmethods to manage thread lifetime, it is recommended to use it with a
context manager (i.e. thewithkeyword).ExamplefromthreadswitchimportPersistentContextimportsqlite3definit_sqlite_ctx(ctx:PersistentContext):ctx.db=sqlite3.connect('my.db')defclean_sqlite_ctx(ctx:PersistentContext):ctx.db.close()withPersistentContext()asctx:@ctx.taskdefexecute(sql,params):ctx.db.execute(sql,params)# Note: As sqlite3.Cursor objects are also thread-bound, operating on one requires a more complex proxy wrapper.# This is skipped in this simple example usage.execute('DROP TABLE foobar').join()RebootContextA context which automatically terminates its background thread once it's processed its pending task queue. This is
useful to simply synchronize access to a common system resource, such as the CLI. When closed, dispatching a new task
will automatically restart the background thread.The thread can be kept alive past the completion of the queue. This is useful to avoid unnecessary thread creation and
termination overhead in a slowly paced environment. It is recommended to use a timeout of a fraction of a second in
general.ExamplefromthreadswitchimportRebootContextfromsysimportstdout,stdinctx=RebootContext(task_wait_time=.1)writeline=ctx.task(stdout.writeline)readline=ctx.task(stdin.readline)writeline('foobar')value=readline().result()CommonThe above sections outline the differences between the two currently implemented context types, but they share various
common behaviors.TasksAs seen in the above examples, one may usectx.taskto create a new dispatchable task. Internally, this simply creates
a wrapper function which callsctx.dispatch(*args, **kwargs)upon invocation. They can be used in two different styles:
threaded and asyncio styles.Thectx.taskmethod can also be used as a decorator as follows:fromthreadswitchimportRebootContextfromsysimportstdinctx=RebootContext(0.5)@ctx.taskdefread(n:int=-1):ifn==-1:returnstdin.readline()returnstdin.read(n)read().result()Threaded StyleThe examples above use "threaded usage style." This style usesjoin,result, andexceptionmethods on the task
objects returned by dispatched tasks to synchronize threads.joinsimply waits until the task completes either by
returning a result value, or by raising an exception.resultretrieves the return value of the task, andexceptionretrieves any occurred exception encountered during the task if applicable. If the task is incomplete, implicitlyjoins.All of these methods support atimeoutargument which may be eitherNoneor a number in seconds.AsyncIO StyleAlternatively, one may simplyawaitany dispatched task. This behaves as one would expect from any otherawaitcall:
If the task was successful, retrieves the result value. If the task encountered an exception, raises the exception in
this thread. As this syntax does not support a timeout, one may alternatively useawait asyncio.wait_for(..., timeout=...).LicenseMIT LicenseCopyright (c) 2021 KirusePermission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.