35. Python Threads

Source: Internet
Author: User

In Python, each process has a Gil lock, causing each process to have only one thread to execute at the same time

Threading Module

Import Threadingimport timedef foo (x):    print ("--------%s"%x)    Time.sleep (3) t1=threading. Thread (target=foo,args= (2,))   #创建线程对象; task, parameter, tuple form T1.start ()  # start print ("Ending")

  

Join

Blocks the main thread, waits for the thread to end before continuing to execute the main thread

Import Threadingimport timedef foo (x):    print ("--------%s"%x)    Time.sleep (3) def bar (n):    print ("--------% S "%n)    Time.sleep (5) s = Time.time () t1=threading. Thread (target=foo,args= (2,))   #创建线程对象; task, parameter, tuple form T1.start ()  # start t2=threading. Thread (target=bar,args= (5,))   #创建线程对象; task, parameter, tuple form T2.start ()  # start T1.join () T2.join () print ("Ending") print ( Time.time ()-s) #----------------AA ending# 5.001526832580566

Do not open multi-threaded run time

Import Threadingimport timedef foo (x):    print ("--------%s"%x)    Time.sleep (3) def bar (n):    print ("------- -%s "%n)    Time.sleep (5) s = time.time () foo (2) bar (5) print (" Ending ") print (Time.time ()-s) #----------------AA ending# 8.000638246536255

Setdaemon (True) daemon thread

The thread is declared as a daemon thread and must be in the start ()
The method is set before the call, and if not set to the daemon, the thread is suspended indefinitely.
When we execute a main thread in a program run, if the main thread creates another child thread, the main thread and the child thread
Just suited two, run separately, then when the main thread completes
When you want to exit, the child thread is checked for completion. If the child thread is not completed, the main thread waits for the child thread to complete before exiting. But sometimes what we need is just the main thread
Completed, regardless of whether the child thread is completed, and the main thread to exit, then you can
With the Setdaemon method.

When Daemon is set to true, if the main thread exits, the child thread will also exit, and the child thread will continue to run until normal exit
  

Import Threadingimport timedef foo (x):    time.sleep (3)    print ("--------%s"% x) def bar (n):    time.sleep (5)    print ("--------%s"% n) s = time.time () t1=threading. Thread (target=foo,args= (2,))   #创建线程对象; task, parameter, tuple form # T1.setdaemon (True) T1.start ()  # start t2=threading. Thread (target=bar,args= (5,))   #创建线程对象; task, parameter, tuple form T2.setdaemon (True) T2.start ()  # start print ("Ending") print ( Time.time ()-s) # ending# 0.0010097026824951172#--------2

  

Thread instance object's method  # isAlive (): Returns whether the thread is active.  # getName (): Returns the thread name.  # SetName (): Sets the thread name. Some of the methods provided by the threading module are:  # threading.currentthread (): Returns the current thread variable.  # 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.  # Threading.activecount (): Returns the number of running threads with the same result as Len (Threading.enumerate ()).

  

==================

Compute-intensive and IO-intensive

IO-intensive:

There are a lot of IO operations in the program

Computationally dense:

There are a number of computational operations (non-IO operations) in the program

For compute-intensive, multithreading is not as sequential as execution; Gil Locks can only come out one thread, and multiple threads can only switch execution, so that the computational-intensive efficiency becomes lower

For IO-intensive, the switch executes other threads for IO blocking, which saves blocking time and high efficiency

Conclusion: For Python, the same process can not take advantage of the multi-core advantage, multithreading processing IO-intensive tasks, has the advantage, for processing computationally intensive tasks, is not recommended to use

35. Python Threads

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.