Python multi-threaded learning record

Source: Internet
Author: User

1. Multi-threaded creation

Import threading

t = t.theading.thread (target, args ...)

T.setdeamon (True)//Set as Daemon

T.start (), starting thread

T.join (), which blocks the current thread, and does not exit even if the current thread ends. Will not exit until the child thread finishes.

Without a join statement, the main thread does not wait until the child thread ends, but does not immediately kill the thread.

However, if you add Setdaemon (True), if you do not join, the child thread will be killed immediately after the main thread ends.

If join () adds time, it will exit after a period of time.

2. Thread Lock and Threadocal

(1) Thread lock

The most important feature of multithreading is that the data can be shared between threads, then there will be a change of a variable between multithreading, such as deadlock, data confusion and so on.

For example, there is a global a, and global B has two threads th1,th2 that happen at some point:

Th1 has a, but needs to access B, and Th2 has B, but needs to access a.

At this time th1,th2 are not, and finally they both starved. This is the deadlock.

In response to the above problem, lock is used to lock resources with Lock.acquire () before accessing a resource, and then Lock.release () to release resources after access.

(2) Theadlocal

Local variables can be used when you do not want to share variables to other threads, but local variables defined in the function make it particularly troublesome to pass between functions, and theadlocal is especially awesome.

He solves two problems in which global variables need to be locked, and local variables are troublesome to pass.

Local_school = Theading.local ()

Defines a Theadocal object, at which point the Local_school is a global variable, but the global variable can be a global variable in that thread, and on other threads it is a local variable that cannot be changed by another thread.

Local_school = Theading.local ()

def process_thread (name): #绑定ThreadLocal的student

Local_school.student = Name

This can only be modified by this thread and not by other threads.

Threadlocal can be understood as a dict that can bind different variables.

The most common use of threadlocal is that each thread processes an HTTP request, which is the principle in the Flask framework, which uses Werkzeug-based localstack.

3.MAP Implementation Multithreading

URLs = [' www.google.com ', ' www.luoxiaofeng.com ', ' www.stackoverfolw.com ']

Results = map (urllib2.urlopen, URLs)

Map passes each element as an argument to the Urllib2.urlopen function, and finally puts the result in the results list. Map handedly a series of operations such as sequence manipulation, parameter passing, and result saving.

The principle is to give each link in the Ruls to a different CPU. The map function is responsible for splitting the threads to different CPUs.

There are two libraries in Python that contain the map function: Mutilprocessing and its subpackage multiprocessing.dummy. Dummy is a complete clone of a multiprocessing module, the only difference is the multiprocessing action and process, while the dummy module acts as a rain thread.

Pool = ThreadPool () #创建县城池

Results = Pool.map (urllib2.urlopen, URLs) #将不同的url传给各自的线程 and returns the execution result to results.

Print results

Pool.close ()

Pool.join ()

4.Python Multi-Threading flaw:

Not really multi-threading, because there is a Gil, the global explanation lock, the existence of the lock ensures that only one thread is actually performing the task at the same time, that is, multithreading is not really concurrent, just alternating execution. If there are 10 threads on 10 CPUs, the current work can only be a thread on a CPU.

5. Application Scenario:

Suitable for IO-intensive tasks. Because I/O intensive execution is spent most of the time on I/O, such as database I/O, less time is spent on CPU computing. Therefore, this scenario can use Python multi-threading, and when a task is blocked on an IO operation, we can immediately switch to other IO operation requests on other threads.

Python multi-threaded learning record

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.