Introduction to two Python thread programming methods

Source: Internet
Author: User

Because there are too many demos for Python Thread Programming, you cannot upload them here. Therefore, if you have a communitysever, you can obtain it from it and decompile it for your own use. If you do not have one, search for it on the network, there are many resources for everyone to learn and think about.

To use a thread in Python Thread Programming, python lib provides two methods. One is a function type, and the other is a thread object encapsulated by a class. Here are two simple examples to help you. For more information about multithreaded programming, such as mutex, semaphore, and critical section, see the Python thread programming documentation and related materials.

1. Call the start_new_thread () function in the thread module to generate a new thread. Please refer to the Code:

 
 
  1. # Thread_example.py
  2. Import time
  3. Import thread
  4. Def timer (no, interval): # self-written thread function
  5. While True:
  6. Print 'thread :( % d) Time: % s' % (no, time. ctime () time. sleep (interval)
  7.  
  8.  
  9. Def test (): # Use thread. start_new_thread () to generate two new threads.
  10. Thread. start_new_thread (timer, (1, 1 ))
  11. Thread. start_new_thread (timer, (2, 3 ))
  12.  
  13.  
  14. If_ Name __= '_ Main __':
  15. Test ()

This is thread. start_new_thread (function, args [, kwargs]) function prototype. The function parameter is the thread function to be called. The args parameter is the parameter that is passed to your thread function, it must be a tuple type, and kwargs is an optional parameter. The end of a thread generally depends on the natural end of a thread function. You can also call a thread in a thread function. exit (), which throws SystemExit exception to exit the thread.

2. Call the threading module to inherit the threading. Thread class to encapsulate a Thread object. See the Code:

 
 
  1. Import threading
  2. Import time
  3. Class timer (threading. Thread): # My timer class inherits from the threading. Thread class.
  4. Def _ init _ (self, no, interval ):
  5. # When I override the _ init _ method, remember to call the _ init _ method of the base class.
  6. Threading. Thread. _ init _ (self)
  7. Self. no= No
  8. Self. interval= Interval
  9. Def run (self): # rewrite the run () method and put the code of your thread function here
  10. While True:
  11. Print 'thread Object (% d), Time: % s' % (self. no, time. ctime ())
  12. Time. sleep (self. interval)
  13. Def test ():
  14. Threadone=Timer() # Generate two thread objects
  15. Threadtwo=Timer(2, 3)
  16. Threadone. start () # activate a thread by calling the. start () method of the thread object
  17. Threadtwo. start ()
  18. If_ Name __= '_ Main __':
  19. Test ()

In fact, the thread and threading modules also contain many other things about multithreading programming, such as locks, timers, and list of activated threads. Please refer to the Python thread programming documentation carefully!

  1. How to embed Python into C ++ applications?
  2. In-depth discussion of Ruby and Python syntax comparison
  3. Introduction to Python
  4. Python Learning Experience: version, IDE selection and coding Solutions
  5. Analysis of Python GIL and thread security

Related Article

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.