threads and concurrency import threading from queue import queue use from queue on python class workerthread(threading thread)def _init_ (self,*args,**kwargs)threading thread _init_ (self,*args,**kwargsself input_queue queue(def send(self,item)self input_queue put(itemdef close(self)self input_queue put(noneself input_queue join(def run(self)while trueitem self input_queue get(if item is nonebreak process the item (replace with useful workprint(itemself input_queue task_done(done indicate that sentinel was received and return self input_queue task_done(return example use workerthread( start( send("hello" send("world" close(send items to the worker (via queuethe design of this class has been chosen very carefully firstyou will notice that the programming api is subset of the connection objects that get created by pipes in the multiprocessing module this allows for future expansion for exampleworkers could later be migrated into separate process without breaking the code that sends them data secondthe programming interface allows for thread termination the close(method places sentinel onto the queue whichin turncauses the thread to shut down when processed finallythe programming api is also almost identical to coroutine if the work to be performed doesn' involve any blocking operationsyou could reimplement the run(method as coroutine and dispense with threads altogether this latter approach might run faster because there would no longer be any overhead due to thread context switching coroutines and microthreading in certain kinds of applicationsit is possible to implement cooperative user-space multithreading using task scheduler and collection of generators or coroutines this is sometimes called microthreadingalthough the terminology varies--sometimes this is described in the context of taskletsgreen threadsgreenletsetc common use of this technique is in programs that need to manage large collection of open files or sockets for examplea network server that wants to simultaneously manage , client connections instead of creating , threads to do thatasynchronous / or polling (using the select moduleis used in conjunction with task scheduler that processes / events lib fl ff