Use of threads in Python

Source: Internet
Author: User

After the thread starts, a function is executed. After the function is executed, the thread ends and the function is executed only once.
Below I will give some explanations in the form of a question.

How can I implement this function to keep threads running? There are two methods:
(1) In the function executed by this thread, set an endless loop, that is, while true. When the thread calls the function, the function will be in an endless loop. After certain conditions are met, you can exit the loop through break. This method is used for the "efficient download of two threads" and the code in the "multi-thread overview" described in this blog.
(2) In the run method of the override thread, set an endless loop, that is, while true. The other is similar to method 1.

So how can we implement a thread to be executed at intervals, that is, to trigger the thread regularly?
You can set a sleep method in the thread loop, such as time. sleep (30), the thread slews every 30 seconds, giving up CPU usage, and joining the thread queue after 30 seconds to wait for the call. That is to say, it achieves our scheduled thread triggering effect.

It is very convenient for python to use threads. It can be used immediately by calling the threading module.

The following is a demo of thread in Python, which can be copied and saved for running. In this demo, You can override the run method, use while true in run to loop, and use sleep (30) in run to implement timed triggering.

#! /Usr/bin/Python #-*-coding: UTF-8-*-import sys; import OS; import time; import threading; Class mtimerclass (threading. thread): # cookie monitoring clock def _ init _ (self, FN, argS = (), sleep = 1): threading. thread. _ init _ (Self) self. fn = FN self. ARGs = ARGs self. sleep = sleep self. setdaemon (true) self. isplay = true # whether to run self. fnplay = false # self. thread_stop = false; def setsleep (self, sleep): # reset the time interval self. Sleep = sleep; def _ Do (Self): Self. fnplay = true; apply (self. FN, self. ARGs); self. fnplay = false def run (Self): While self. isplay: If self. thread_stop = true: break; # If subcommon. ifexestop = true: # You can use an external call to disable the thread. # Print 'thread break' # Break; # print self. sleep; time. sleep (self. sleep) self. _ Do (); def stop (Self): # Stop the loop self. thread_stop = true; self. isplay = false; While true: If not self. fnplay: break time. sleep (0.01) def getsearchinfo (): Print 'hhhhh... '; # To do pass; def main (): tcheck = mtimerclass (getsearchinfo, '', 60); tcheck. setdaemon (true); # result tcheck along with the main thread. start (); # Start thread if _ name __= = "_ main _": Main ();

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.