Python multithreading-thread module, python multithreading-thread

Source: Internet
Author: User

Python multithreading-thread module, python multithreading-thread

The thread and threading modules can implement multiple threads in python. In general, it is more convenient to use threading because thread has many disadvantages. For example, when the main thread ends, the subthread will be forcibly terminated, no warning or normal cleaning. Therefore, the threading module is generally recommended. However, for the purpose of learning, let's take a look at both modules.

Before learning the code, we should first understand python's GIL, that is, the global interpreter lock. This lock ensures that only one thread can run at a time.

Wait ...... I clearly want to use multithreading. Why does this lock ensure that only one thread is running? Isn't it concurrent? Isn't that a pitfall?

In fact, we know that python is a general explanatory language, that is, our code can only run after being interpreted by the interpreter, while a python process has only one python interpreter. We know that the cpu schedules threads unordered and random, so we cannot guarantee the execution sequence of the Code. For example, I declare a variable before and use this variable later. However, during cpu scheduling, the code behind the variable runs first, but our variable is not declared at this time, this will certainly lead to various bugs. GIL exists to ensure that the code execution is not chaotic. In a multi-threaded environment, the Python virtual machine is executed as follows:

1. Set GIL

2. Switch to a thread to run

3. Run:

A. specify the number of bytecode commands, or

B. The thread actively controls outbound traffic (time. sleep (0) can be called ))

4. Set the thread to sleep

5. Unlock GIL

6. Repeat all the above steps again

That is to say, I have segmented the code and put the relevant code in one piece, so that the dependent code can be executed together, And the unrelated code can be executed separately. However, in general, there is only one interpreter running, and only one piece of code can be run at the same time, except for the separation.

Therefore, many people say that the multi-threading of python is a chicken rib, and the role is not too great. But it cannot be completely said.

For example, for all I/O-oriented (the built-in operating system C code will be called) programs, GIL will be released before this I/O call, to allow other threads to run while this thread is waiting for I/O. If a thread does not use many I/O operations, it will occupy the processor (and GIL) in its own time slice ). That is to say, I/O-intensive Python programs make full use of the advantages of multi-threaded environments than computing-intensive programs.

Multithreading is suitable for file read/write operations or when crawlers are waiting for the download of webpages.

After talking about how python implements multithreading and how to use multithreading, we will start learning the module.

 

 

Thread Module

In my previous discussions, when learning any python content, call the help () function to view its built-in help documentation. The help document contains a lot of content. Let's take a look at the FUNCTIONS section.

1. allocate ()/allocate_lock ()

We can see that the descriptions are the same. Allocate_lock ()-> lock object

Creates a lock object. For more information, see the LockType section.

At the same time, we also need to say that allocate () is an obsolete synonym, that is, the previous method is an outdated syntax, so we should use allocate_lock.

 

2. exit ()/exit_thread ()

Exit_thread () is an obsolete synonym. Similarly, we can use exit.

It is synonymous with raise SystemExit and will exit a thread unless exception capture is performed.

At the same time, the thread exit method in python is also described here:

1. Call exit functions such as thread. exit ().

2. Use the standard method of exit the process using python, sys. exit (), and even the process exits, the thread disappears.

3. A SystemExit exception is thrown.

Of course, we have a way to set the daemon thread, but this is the content in the threading module.

 

3. get_ident ()-> integer

Returns a non-zero integer that uniquely identifies both the current thread and other threads.

This can be used to identify each thread resource. On Some platforms, the identity for allocating threads starts with a continuous number, but this is not absolute. The identity of one thread may be reused by another thread after exiting.

 

4. interrupt_main ()

A KeyboardInterrupt exception is thrown in the main thread.

The sub-thread can use this method to interrupt the main thread.

 

5. stack_size ([size])-> size

Returns the size of the thread stack used to create a new thread.

You can use the size parameter to specify the stack size (in bytes) and then create the corresponding thread. It must be 0 (by default using the platform or configuration) or a positive integer with a value of at least 32768 (32 k ). If the thread stack size cannot be changed, a ThreadError exception is triggered. If the specified value is invalid, ValueError is triggered without modifying the value. 32 k bytes are currently the minimum supported stack size values, in order to ensure that there is sufficient stack space to translate itself. Note: Some platforms may have special stack size restrictions. For example, the minimum heap size must be greater than 32 kb or the size must be a multiple of the system memory page size. For details, see this system platform description. Generally, 4 kb is the most common on each page. You can try to use a multiple of 4 kb when there is no description.

Generally, the method is too low-level.

  

6. start_new (function, args [, kwargs])/start_new_thread (function, args [, kwargs])

Start_new () is an obsolete synonym, so you can use start_new_thread.

Start a new thread and return its identifier. The thread calls the corresponding function and uses the element in The args ancestor as the location parameter. The dictionary kwargs is passed in as the keyword parameter. When the function returns, the thread ends, but its return value is ignored. When an unprocessed exception is triggered inside the function, the thread ends and the stack trace information is printed. Of course, the system exit is triggered without any trace.

You must have the first two parameters. Therefore, even if the function we want to run is not a parameter, we need to pass an empty tuples.

 

It can be seen that a function is executed as a whole, that is, a code block. What we need to do is to encapsulate our business logic into a function, which can call other functions and create class instances.

To sum up with a picture:

 

Sample code from a region:

Def loop1 (): print 'subthread 1 start: ', ctime () sleep (4) print 'subthread 1 end:', ctime () def loop2 (): print 'subthread 2 Start: ', ctime () sleep (2) print 'subthread 2 end:', ctime () def main (): print' main thread start: ', ctime () thread. start_new_thread (loop1, () thread. start_new_thread (loop2, () sleep (6) # To prevent the main thread from stopping, it waits for 6 seconds, which is the sum of the execution time of the two functions: 4 + 2 = 6 print 'main thread end: ', ctime () if _ name _ =' _ main _ ': main ()

 

Output:

We can see that the result of Multithreading is indeed achieved, but we have added sleep (6) to prevent the main thread from exiting. If we do not add it, the result is as follows:

The main thread does not wait for the sub-thread to execute. After the main thread completes execution, the process exits and the sub-thread disappears. In order to prevent this situation, we let the main thread wait, but here, because I know that the total execution time of the two threads will not exceed 6 seconds, in fact, it is calculated based on the longest function used,Greater4 seconds is enough, but when we change to sleep (4), the result is as follows:

The execution is completed successfully.

The main thread exits first.

That is to say, the time card is too tight and the chances of success are not necessarily. It depends on whether the cpu schedules the subthread or the main thread first, but the scheduling sequence of the cpu is unknown.

However, the time required for executing the function is usually uncertain, and the time required varies depending on the cpu performance. What should we do at this time?

Use the thread lock, that is, the allocate_lock () method. This method returns a LockType Lock Object. Next we will learn what LockType is.

LockType

The help documentation is still available in the thread module. After several built-in methods and repeated methods are removed, only three methods are available. Let's take a look.

1. acquire ([wait])-> bool

Lock the code block. If the thread does not unlock the code block after the lock, the thread will be blocked.

If no parameter exists, the lock is applied and the other thread is locked. If the other thread is locked, True is returned.

If the given parameter is true, the lock is performed only when the Boolean value of the parameter is true, and a value is returned to indicate whether the lock is successful.

Blocking is not interrupted.

 

2. release ()

Release the lock so that another blocked thread can obtain the thread lock. The lock object must be locked, but it does not need to be unlocked by the same thread.

 

3. locked ()-> bool

Returns a Boolean value to determine whether the Lock Object is locked.

 

Code example in a region:

 

Loops = [4, 2] def loop (nloop, nsec, lock): print 'subthread', nloop, 'start: ', ctime () sleep (nsec) print 'subthread', nloop, 'end: ', ctime () lock. release () # unlock def main (): print 'main thread start: ', ctime () locks = [] # A list of lock objects used to store nloops = range (len (loops) for I in nloops: lock = thread. allocate_lock () # create a process lock Object lock. acquire () # try to obtain the Lock Object locks. append (lock) # Put the obtained lock Object in a list for I in nloops: thread. start_new_thread (loop, (I, loops [I], locks [I]) # Start a new thread for I in nloops: while locks [I]. locked (): # check whether all thread locks are released. Otherwise, the thread continues to run cyclically to prevent the main thread from stopping and causing the sub-thread to Exit pass print 'main thread end: ', ctime () if _ name _ = '_ main _': main ()

 

Because the two threads start at the same time, the line printed at the beginning looks a little messy.

 

For more information about threading, enter it in the next 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: 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.