Python Multi-Threading: Threading Module

Source: Internet
Author: User
Tags thread class

Tag:__init__   target    time period    odi   init     tips    live   self    concurrency    

1, the threading module provides the class Thread,lock,rlock,condition,semaphore,event,timer,local2, The common method provided by the threading module (1) threading.currentthread (): Returns the current thread variable. (2) Threading.enumerate (): Returns a list that contains the running thread. Running refers to threads that do not include pre-and post-termination threads until after the thread has started and ends. (3) Threading.activecount (): Returns the number of running threads, with the same result as Len (Threading.enumerate ())   One, thread Class 1, t1=threading. Thread (target=,name=,args=,kwargs=) parameter description:? Group: Thread groups, currently not implemented, the library reference must be None;?target: the method to execute; Name: thread name;? args/ Kwargs: The parameter to pass in the method.  import threadingimport timedef func1 (a=none,b=none):p rint a,btime.sleep (1)  t1=threading. Thread (' Hello ', ' world ')//This sentence just creates a thread and does not execute the thread, at which point the target=func1,args= is in the new state. Whether the t1.isalive () thread is running the t1.getname fetch thread name t1.setname (' test1 ') sets the thread name T1.start () to start the thread, at which point the thread is still not running, just in a ready state. T1.join () Wait for the thread to finish executing the   process is the main thread 2, the daemon thread T1.setdaemon (True) when the main process execution ends, if it is a daemon thread, and the daemon thread does not end, also with the main thread exit # encoding= Utf-8import threadingimport time def func1 (A=none, B=none):p rint a,bprint ' Enter Func1 ' Time.sleep (5) print ' exit Func1 '  T1 = threading. Thread (TARGET=FUNC1, args= (' Hello ', ' World! ')) Print t1.isalive () print t1.getname () T1.setdaemon (True) print ' daemon: ', T1.isdaemon () t1.setname (' test1 ') print T1.getname () T1.start () Time.sleep (1.1) Print t1.isalive () print ' main thread end '  3, Threading.currentthread () Main thread Mainthread two, multi-threaded programming Way 1, create the thread of the way one: thread, a new thread instance, through the target incoming execution flow. The parameter is passed through the Args method two: The thread class is overridden by T1.start () (internally called Run ()). So you can rewrite the run () method to achieve the effect we want  2, way two: rewrite the thread class import Threadingclass Mythread (threading. Thread):d EF __init__ (self,a): Threading. Thread.__init__ (self): Self.a=adef run (self):p rint ' Now Sleep ', self.a, ' seconds ' time.sleep (a) print ' Sleep end '   T1=mythread (3) T2=mythread (2) T1.start () T2.start () T1.join () T2.join ()   #隐含问题: How does the main thread get the execution results of threads? (1) Through the way of the queue (2) through the way of global variables  3, concurrency and parallel concurrency refers to a period of time to run simultaneously, representing an interval and parallel refers to at the same point in time is running, is a point, and concurrently at the same point in time there can be only one program running the two relationships of concurrent threads: synchronous and mutex.   Thread pool from multiprocessing.dummy import pooldef func (a): Time.sleep (1) Print AIF __name__== ' __main__ ': lista=[1,2, ' A ', ' B ', ' 5 ']pool=pool (5) (Func,lista) pool.close () #线程池不接受新线程的请求pool. Join () #Wait for all threads to finish  

Python Multi-Threading: Threading Module

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: 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.