Explanation of main expressions of Python Thread Programming

Source: Internet
Author: User

Python thread programming is widely used in computer language applications. If you are confused about the expressions in Python Thread Programming, you can browse our article, our article is a concrete expression of the two expressions, and I hope you will get some benefits.

To use a thread in Python, python lib provides two methods. One is a function type, and the other is a thread object encapsulated by a class. I would like to give two simple examples. For more information about Python multi-threaded programming, such as mutex, semaphore, and critical section, please refer to the python documentation and related materials.

1. Call the start_new_thread () function in the thread module to generate a new thread,

See the Code:

 
 
  1. Python code
  2.  
  3. ### Thread_example.py
  4.  
  5. Importtime
  6. Mportthread
  7. Deftimer (no, interval): # self-written thread function
  8. WhileTrue:
  9. Print 'thread :( % d) Time: % s' % (no, time. ctime ())
  10.  
  11. Time. sleep (interval)
  12. Deftest (): # Use thread. start_new_thread () to generate two new threads.
  13. Thread. start_new_thread (timer, (1, 1 ))
  14. Thread. start_new_thread (timer, (2, 3 ))
  15. If _ name __= '_ Main __':
  16. Test ()
  17.  

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 of the tuple type, and kwargs is an optional parameter. The end of a thread generally ends naturally by the thread function. You can also call thread. exit () in the thread function to throw SystemExit exception to exit the Python thread programming.

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

 
 
  1. Python code
  2.  
  3. Importthreading
  4. Importtime
  5. Classtimer (threading. Thread): # My timer class inherits from the threading. Thread class.
  6. Def _ init _ (self, no, interval ):
  7. # When I override the _ init _ method, remember to call the _ init _ method of the base class.
  8. Threading. Thread. _ init _ (self)
  9. Self. no= No
  10. Self. interval= Interval
  11. Defrun (self): # rewrite the run () method and put the code of your own thread function here
  12. WhileTrue:
  13. Print 'threadobject (% d), Time: % s' % (self. no, time. ctime ())
  14. Time. sleep (self. interval)
  15. Deftest ():
  16. Threadone=Timer() # Generate two thread objects
  17. Threadtwo=Timer(2, 3)
  18. Threadone. start () # activate a thread by calling the. start () method of the thread object
  19. Threadtwo. start ()
  20. If _ name __= '_ Main __':
  21. Test ()
  22.  

In fact, the thread and threading modules also contain many other things about Python thread programming, such as locks, timers, and list of activated threads. Please refer to the python documentation carefully! I hope you will have some gains.

  1. Comparison between Python programming language and Ruby
  2. Summary of advantages in the use of Python Programming Language
  3. Detailed introduction to python multi-thread applications
  4. The most basic formula of the Python list Example
  5. The wonderful use of the Python programming language in website development

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.