"Python" python multithreading two ways to implement

Source: Internet
Author: User

At present, Python provides several ways to implement multi-threading thread,threading,multithreading, where the thread module compared to the bottom, and the threading module is the thread to do some packaging, can be more convenient to be used.
Prior to version 2.7, Python's support for threading was not complete enough to take advantage of multicore CPUs, but the 2.7 version of Python had already considered improving this and the multithreading module appeared. The threading module is mainly about manipulating some threads, creating the class of thread. In general, there are two modes of using threads:
A Create a function to execute the thread, pass the function into the thread object, and let it execute;
B inherit the thread class, create a new class, and write the code that will be executed into the run function.
This article describes two methods of implementation.
The first creates a function and passes in the thread object
t.py Script Content

  1. Import Threading,time
  2. From time import sleep, CTime
  3. def now ():
  4. Return str (time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime ()))
  5. def test (Nloop, nsec):
  6. print ' Start loop ', Nloop, ' at: ', now ()
  7. Sleep (nsec)
  8. print ' Loop ', Nloop, ' done on: ', Now ()
  9. def main ():
  10. print ' Starting at: ', now ()
  11. Threadpool=[]
  12. For I in Xrange (10):
  13. th = Threading. Thread (target= test,args= (i,2))
  14. Threadpool.append (TH)
  15. For th in ThreadPool:
  16. Th.start ()
  17. For th in ThreadPool:
  18. Threading. Thread.Join (TH)
  19. print ' All do at: ', now ()
  20. if __name__ = = ' __main__ ':
  21. Main ()

Execution Result:

thclass.py Script content:

  1. Import threading, Time
  2. From time import sleep, CTime
  3. def now ():
  4. Return str (time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime ()))
  5. Class MyThread (threading. Thread):
  6. "" "DocString for MyThread" ""
  7. def __init__ (self, Nloop, nsec):
  8. Super (MyThread, self). __init__ ()
  9. Self.nloop = Nloop
  10. Self.nsec = nsec
  11. def run (self):
  12. print ' Start loop ', Self.nloop, ' at: ', CTime ()
  13. Sleep (self.nsec)
  14. print ' Loop ', Self.nloop, ' done at: ', CTime ()
  15. def main ():
  16. Thpool=[]
  17. print ' Starting at: ', now ()
  18. For I in Xrange (10):
  19. Thpool.append (MyThread (i,2))
  20. For th in Thpool:
  21. Th.start ()
  22. For th in Thpool:
  23. Th.join ()
  24. print ' All do at: ', now ()
  25. if __name__ = = ' __main__ ':
  26. Main ()

Execution Result:

"Python" python multithreading two ways to implement

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.