Python Multithreading threading Usage

Source: Internet
Author: User

Python often uses multiple threads, which means that all methods start at the same time, rather than sequentially

run. The module used is threading, and the following is a detailed threading usage.

    • We write three methods, one,two,three and run normally.

There's only one () method,two,three, and the same content.

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/9D/6B/wKiom1l_9KWTCtTgAAAp1kCJJF8990.png-wh_500x0-wm_ 3-wmp_4-s_4059096748.png "Title=" write three methods. png "alt=" wkiom1l_9kwtcttgaaap1kcjjf8990.png-wh_50 "/>

As shown in the figure below, three functions are run at different times, respectively.

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/9D/6B/wKiom1l_9LKz7NtCAABtOuTkVXM865.png-wh_500x0-wm_ 3-wmp_4-s_2315058694.png "Title=" runs three methods at different times. png "alt=" wkiom1l_9lkz7ntcaabtoutkvxm865.png-wh_50 "/>

    • We use threading . make three methods run at the same time

Define a thread pool and write the threads that you want to run to this thread pool list:

Threads= [] #define a pool of threads
T1= Threading. Thread (Target= One,args=(,)) #Create a thread and assign it toT1, this thread specifies the calling method One, with no parameters
Threads. Append (t1)#putT1thread loaded intoThreadsLine Constructor
T2= Threading. Thread (Target=Both )
threads. Append (T2)
T3
= Threading. Thread (Target=three)
threads. Append (T3)

At this point threads has three threads in the list.

The following is the thread that runs inside this thread pool

 forTinchThreads:
T.setdaemon (True)#StatementTfor the daemon thread, the child thread will run with the main thread and end directly, no more sub-threads inside the loop will be executed.
T.start ()
T.join ()
#function is to execute all child threads before executing the main thread

use a for statement to traverse the thread in the threads, and then call the start () method to run

Note that t.join () must be placed outside the for statement.

    • The result of the operation is started at the same time

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/9D/6B/wKioL1l_9L2SOoqzAAArsXnj3cQ747.png-wh_500x0-wm_ 3-wmp_4-s_3111532750.png "title=" run result. png "alt=" wkiol1l_9l2sooqzaaarsxnj3cq747.png-wh_50 "/>

is it quick to understand the use of threading?

there are problems to communicate QQ Group: 610845268


This article is from the "It Bug" blog, so be sure to keep this source http://laomomo.blog.51cto.com/6595318/1952603

Python Multithreading threading Usage

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.