Python - Timer Threads

來源:互聯網
上載者:User

標籤:

 

Timer starts its work after a delay and can be 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 be made to run faster by applying techniques for running parts of the job concurrently 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 forth 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 are processed asynchronously, usually by interrupting what the program is doing when the signal arrives. Signalling is useful as a coarse messaging system, but other inter-process communication techniques are 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. Using threads is an easy way to scale for tasks that are more I/O bound than CPU bound.

The multiprocessing module mirrors threading, except that instead of a Thread class it provides a Process. Each Process is a true system process without shared memory, but multiprocessing provides features for sharing data and passing messages between them. In many cases, converting from threads to processes is as simple as changing a few import statements.

 

import threadingimport timeimport logginglogging.basicConfig(level=logging.DEBUG,    format=‘(%(threadName)-10s) %(message)s‘,)def worker():    while 1:        time.sleep(3)        logging.debug(‘worker running‘)threads = []for i in range(2):        # t = threading.Thread(target=worker)    t = threading.Timer(1, worker)    threads.append(t)    t.start()    logging.debug(‘loop running‘)

 

將threading.Thread對象改為Timer,構造對象的參數也改為相應的(延遲時間, 函數名).

比較重要的是綠色背景的代碼,這個是用於調試。這也提醒了我,線程是進程的一個“邏輯”分支。

調試資訊如下:

(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

  

以下是我自己用畫圖的理解:

 

Python - Timer Threads

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.