Understand how to create a program with multiple threads in Python

Source: Internet
Author: User

Python multithreading plays an important role in program design and is more important in the deamon program of the server. However, in that system, the overhead of multi-thread scheduling in Python is much higher than that in other systems. If you want to have a better understanding of this, you can browse the following article. Hope you have some gains.

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. What is lacking in the US is that the Python multi-thread runs on the Python virtual machine, and the created multi-thread may be a virtual thread, which needs to be polling and scheduled by the Python virtual machine, which greatly reduces the availability of the 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

Threading. Thread. join (th)
Or th. join ()

If you can import

 
 
  1. class Thread( group=None, target=None, name=None, args=(),
    kwargs={})   
  2.  

Well divided, and there is no need to communicate between threads, you can use: Create =, run =, recycle to write your Python multi-thread program. 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 and create a lock.

Finally, simulate a multi-thread program for bus and subway IC Card Fare paymentThere are 10 card readers. Each card reader deducts a dollar each time and enters the ledger. Each card reader is charged 10000000 times a day. 100 original accounts. So the final general account should be 10000100. First, comment out the lock code without using the mutex lock.) Let's see what happens.

 
 
  1. G_mutex=Threading. Lock ()
  2. ....
  3. Use lock
  4. For ...:
  5. # Lock, mutual access exclusive from the next code to before release
  6. G_mutex.acquire ()
  7. A_account.deposite (1)
  8. # Release
  9. G_mutex.release ()

The above is an introduction to Python multithreading.

  1. Comparison of python program debugging and c/c ++ Functions
  2. Comparison between python program debugging and c/c ++ Functions
  3. Comparison between python program debugging and c/c ++ Functions
  4. Python logs require constant learning.
  5. Comparison between python program debugging and c/c ++ Functions

Related 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.