A concise example of trying multi-thread programming in Python

Source: Internet
Author: User
This article mainly introduces a concise example of multi-threaded programming in Python. due to the existence of GIL, multi-threaded programming in Python is a hot and difficult issue. For more information, see Summary
Multithreading is an important aspect in programming, especially in the Deamon program of the server. Regardless of the system, the thread scheduling overhead is much faster than the traditional process.
Python supports multiple threads. Allows you to quickly create threads, mutex locks, semaphores, and other elements, and supports synchronization and mutex between read and write threads. In the US, Python runs on a Python virtual machine. the multi-thread created may be a virtual thread and needs to be polling and scheduled by the Python virtual machine, which greatly reduces the availability of Python multi-thread. We hope that later versions of Python can solve this problem and maximize the efficiency of multiple CPUs.
Some friends on the Internet say there are two ways to achieve the benefits of real multi-CPU:
1. you can create multiple processes instead of threads. the number of processes is the same as that of the cpu.
2. using Jython or IronPython, you can get real multithreading.
Let's talk about how to create a thread in Python.
Create a Python thread
Use the Thread class of the threading module
The class interface is as follows:

The code is as follows:

Class Thread (group = None, target = None, name = None, args = (), kwargs = {})


Target and args. target are target functions that require sub-threads to run, and args are function parameters that are passed in the form of tuple.
The following code creates a subthread pointing to the worker function

The code is as follows:

Def worker (a_tid, a_account ):
...
Th = threading. Thread (target = worker, args = (I, acc ));

Start this thread

The code is as follows:

Th. start ()


Wait for the thread to return

The code is as follows:

Threading. Thread. join (th)


Or th. join ()
If you can divide the data to be processed and there is no need to communicate between threads, you can compile your multi-threaded program in the following way: Create = run = recycle. However, if the threads need to access a common object, the mutex lock or semaphore must be introduced to access resources mutex.
The following describes how to create a mutex lock.
Create lock

The code is as follows:

G_mutex = threading. Lock ()
....


Use lock

The code is as follows:

For ...:
# Lock, mutual access exclusive from the next code to before release
G_mutex.acquire ()
A_account.deposite (1)
# Release
G_mutex.release ()


Finally, simulate a multi-thread program for bus and subway IC card fare payment
There are 10 card readers. each card reader deducts a dollar each time and enters the general ledger. each card reader is charged 10000000 times a day. 100 original accounts. So the final general account should be 10000100. First, do not use mutex lock for locking (comment out the lock code) to see the consequences.

import time,datetimeimport threading def worker(a_tid,a_account): global g_mutex print("Str " , a_tid, datetime.datetime.now() ) for i in range(1000000):  #g_mutex.acquire()  a_account.deposite(1)  #g_mutex.release() print("End " , a_tid , datetime.datetime.now() )  class Account: def __init__ (self, a_base ):  self.m_amount=a_base def deposite(self,a_amount):  self.m_amount+=a_amount def withdraw(self,a_amount):  self.m_amount-=a_amount  if __name__ == "__main__": global g_mutex count = 0 dstart = datetime.datetime.now() print("Main Thread Start At: ", dstart) #init thread_pool thread_pool = [] #init mutex g_mutex = threading.Lock() # init thread items acc = Account(100) for i in range(10):  th = threading.Thread(target=worker,args=(i,acc) ) ;  thread_pool.append(th)    # start threads one by one   for i in range(10):  thread_pool[i].start()   #collect all threads for i in range(10):  threading.Thread.join(thread_pool[i]) dend = datetime.datetime.now() print("count=", acc.m_amount) print("Main Thread End at: ", dend, " time span ", dend-dstart)

Note: Do not use mutex lock to control access to critical segments. the running result is as follows:

The results show that the program runs in multiple threads. However, because no mutex access is made to the object Account, the result is incorrect, only 3434612, much less than expected.

After the lock is opened:

This time, we can see that the result is correct. The running time is much longer than not mutex, but this is also the cost of synchronization.
At the same time, it is found that multi-threaded and multi-process programs cannot be run with their own idle. There will be errors.

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.