A Timer starts its work after a delay and can is canceled at any point within that delay time period.
Threading
Python includes sophisticated tools for managing concurrent operations using processes and threads. Even many relatively simple programs can is made to run faster by applying techniques for running parts of the job Concurr ently using these modules.
subprocess provides an API for creating and communicating with secondary processes. It is especially good for running programs that produce or consume text, since the API supports passing data back and Fort H through the standard input and output channels of the new process.
The signal module exposes the UNIX signal mechanism for sending events to other processes. The signals was processed asynchronously, usually by interrupting what the program was doing when the signal arrives. Signalling is useful as a coarse messaging system, but the other inter-process communication techniques was more reliable and Can deliver more complicated messages.
threading includes a high-level, Object-oriented API for working with concurrency from Python. Thread objects run concurrently within the same Process and share memory. The Using threads is a easy-to-have-to-have-to-be-to-be-bound than CPU bound.
The multiprocessing module mirrors threading, except that instead of a Thread class It Provides a Process. Each Process was a true system Process without shared memory, but multiprocessing provides features for s Haring data and passing messages between them. In many cases, converting from threads to processes are as simple as changing a few import statements.
ImportThreadingImport TimeImportLogginglogging.basicconfig ( level=logging. DEBUG, format='(% (threadname) -10s)% (message) s',) defworker (): while1: Time.sleep (3) Logging.debug ('worker Running') Threads= [] forIinchRange (2): #t = Threading. Thread (target=worker) t = Threading.Timer(1 , worker) threads.append (t) T.start () Logging.debug ('Loop Running')
Will threading. The thread object is changed to a timer, and the parameters of the constructed object are changed to the corresponding ( delay time, function name ).
The more important is the green background of the code, this is for debugging. This also reminds me that a thread is a " logical " branch of the process.
The debugging information is as follows:
(Mainthread) loop running (Mainthread) loop running (Thread-2 ) worker running (Thread-1 ) worker running ( Thread-1 ) worker running (Thread-2 ) worker running (Thread-2 ) worker running (Thread-1 ) worker Running (Thread-1 ) worker running (Thread-2 ) worker running
Here is my own understanding of drawing:
Python-timer Threads