How to highlight new features of the language in Python Thread Programming

Source: Internet
Author: User
Tags python list

I am very interested in Python thread programming. I think that when you connect Python thread programming to the queue, you can easily complete thread programming in Python, the following is an introduction to related content. I hope you will gain some benefits after reading this article.

Use Python Thread Programming

By combining threads and queues, you can easily program threads in Python. This article will study how to use both threads and queues to create some simple but effective modes to solve the problem of concurrent processing.
Introduction

For Python, there is no lack of concurrency options. Its standard library includes support for threads, processes, and asynchronous I/O. In many cases, Python simplifies the use of various concurrency methods by creating high-level modules such as Asynchronous, thread, and sub-process. In addition to the standard library, there are also some third-party solutions, such as Twisted, Stackless, and process modules.

This article focuses on the use of Python threads and uses some practical examples. Although many good online resources detail thread APIs, this article attempts to provide some practical examples to illustrate some common thread usage modes.

  • How does a Python game solve the inconvenience caused by game operations?
  • How to reference arrays in the Python list
  • Tips for implementing Java operations in Python"
  • Python multi-dimensional array code "Ultimate Edition"
  • Introduction to Python GUI library better than other toolboxes

Global interpreter Lock indicates that the Python interpreter is NOT thread-safe. The current thread must hold a global lock for secure access to Python objects. Because only one thread can obtain the Python object/c api, the interpreter regularly releases and reacquires the lock every time it passes through 100 bytecode instructions. The interpreter can check the thread switching frequency through the sys. setcheckinterval () function.

In addition, the lock will be released and re-acquired based on potential blocking I/O operations. For more information, see Gil and Threading State and Threading the Global Interpreter Lock in the references section, applications with limited CPU will not be able to benefit from the use of threads. We recommend that you use processes or create processes and threads in a hybrid manner when using Python.

First, it is very important to clarify the differences between processes and threads. The difference between threads and processes is that they share status, memory, and resources. For a thread, this simple difference is both its advantage and Its disadvantage. On the one hand, threads are lightweight and easy to communicate with each other, but on the other hand, they also bring about various problems including deadlocks, contention conditions and high complexity. Fortunately, because of GIL and the queue module, it is much less complex to use the Python language than to use other languages.

Use Python threads

To continue learning the content in this article, I assume that you have installed Python 2.5 or later, because many examples in this article will use the new features of the Python language, these features only appear after Python2.5. To start using a Python thread, we will start with a simple "Hello World" Example:

 
 
  1. hello_threads_example  
  2.  
  3.  
  4. import threading  
  5. import datetime  
  6.  
  7. class ThreadClass(threading.Thread):  
  8. def run(self):  
  9. now = datetime.datetime.now()  
  10. print "%s says Hello World at time: %s" %   
  11. (self.getName(), now)  
  12.  
  13. for i in range(2):  
  14. t = ThreadClass()  
  15. t.start()  

If you run this example, you will get the following output:

 
 
  1. # python hello_threads.py   
  2. Thread-1 says Hello World at time: 2008-05-13 13:22:50.252069  
  3. Thread-2 says Hello World at time: 2008-05-13 13:22:50.252576  

The above section describes how to use Python thread programming.

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.