Package: merethread
What is MereThread?MereThreads are merely python threads (subclasses ofthreading.Thread), plus various
useful features.This package also includes thread classes suitable for common use cases
(specifically,EventLoopThreadandTaskThread).What MereThread isn’t?This isn’t an ambitous new approach to multithreading.  Mere threads, plus added features.
No magic is involved.If you’re familiar with working with standard python threads (thethreading.Threadclass),
there’s almost nothing new to learn before using MereThreads, and benefiting from the
useful features.FeaturesAdded attributes:Thread.result: the value “returned” by the thread (e.g. the result of a computation).Thread.exception: the exception which caused the thread to abort.Future-interface: Aconcurrent.futures.Futureinterface, using theThread.futureattribute.Useful mainly for adding callbacks/errbacks to be called when the thread finishes.Also allows you to wait on multiple threads (usingconcurrent.futures.wait()orconcurrent.futures.as_completed())Clean stopping/cancelling: by callingDaemonThread.stop(), orTaskThread.cancel().This depends on thread’swell-behaved-ness.Debugging and profiling threadsEasily profile a thread.Enable profiling on the thread by passingprofile=True.Access profiler data and stats using theThread.profilerattribute.Easily view the current (live) stack-trace of the thread, using theThread.get_current_stacktrace()method.Access thread execution start/end times, using theThread.runtimeattribute.TheThread.join()method returns a bool indicating whether thread has finishedThis corrects an annoying inconvenience in the interface of the standardThreadclass.Thread ClassesThis package includes definitions ofabstract thread classes, suitable for common use cases.These classes are subclasses of themerethread.Threadbaseclass, and include all the features
listed above.These are:DaemonThread: A thread which is meant to run for as long as the process is alive.Can be signaled to stop (cleanly) by calling itsDaemonThread.stop()method.Exiting prematurely is considered an error, and an appropriate error handler is called, so
they don’t disappear silently.EventLoopThread: A specializedDaemonThread, customized for the common case of running
anevent-loop.A concreteEventLoopThreadsubclass only needs to define how to read the next event, and how
to handle an event.TaskThread: A “temporary” thread which is meant to run a specific task (e.g. compute some value)
and exit.Can be cancelled (cleanly) by calling itsTaskThread.cancel()method.ExpiringTaskThread: A thread which is meant to run for a predefined duration and exit.FunctionThread: A specializedTaskThreadwhich runs a caller-providedtargetfunction
(similar to the standardThreadtargetarguemnt).This class is provided for convenience.  It is not a well-behaved thread.Cancelling aFunctionThreadcan only be done before it starts running.You should prefer subclassingTaskThreadinstead of using aFunctionThreadwhen
possible.Well Behaved ThreadsIn order to support clean stopping/cancelling of threads, the concrete thread subclasses have to adhere
to one basic rule: they have tocheckOFTENif the thread has been signalled to stop/cancel.The frequency of the check defines thread’s responsiveness to stopping/cancelling.
In other words, a check frequency of at most X seconds means it can take up to X seconds, from the time
stop/cancel is requested, until the thread stops (or, more accurately, until it detects it should stop, and
moves on to its exiting-routine).How often isOFTEN?  That depends on the application, but a good rule of thumb, for most applications,
is that a frequency of 200 millis is often enough, and 2 seconds is not.
Care should also be taken not to check too often (e.g. every 0.1 millis), because that would result in a
busy-wait loop, and wasted CPU time.InstallationInstall usingpip:% pip install merethreadOther locationsMereThread’s GitHub pageMereThread on PyPI