Python-timer Threads

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.