( Python Cookbook” Chapter 12, by David Beazley and Brian K. Jones)
- ASIN : B00DQV4GGY
- Publisher : O’Reilly Media; 3rd edition (May 10, 2013)
- Publication date : May 10, 2013
- Language : English
Python has long supported different approaches to concurrent programming, including
programming with threads, launching subprocesses, and various tricks involving gen‐
erator functions. In this chapter, recipes related to various aspects of concurrent pro‐
gramming are presented, including common thread programming techniques and ap‐
proaches for parallel processing.
As experienced programmers know, concurrent programming is fraught with potential
peril. Thus, a major focus of this chapter is on recipes that tend to lead to more reliable
and debuggable code.
When You Use Concurrency with python?
Concurrency is useful in Python when you want to achieve faster execution times and better resource utilization by allowing multiple tasks or operations to be performed simultaneously. In particular, concurrency is well-suited for I/O-bound tasks, where the program spends most of its time waiting for input or output, as well as for CPU-bound tasks, where the program needs to perform computationally intensive operations.
Here are some examples of situations where concurrency can be useful in Python:
- Web scraping: When scraping data from multiple websites, concurrency can be used to request and process data from multiple websites simultaneously, improving the overall performance of the scraper.
- Network programming: When developing network applications, concurrency can be used to handle multiple client connections simultaneously, improving the responsiveness and scalability of the application.
- Data processing: When processing large amounts of data, concurrency can be used to distribute the workload across multiple CPU cores or processors, improving the performance and reducing the time required to process the data.
- GUI programming: When developing graphical user interfaces, concurrency can be used to handle user events and perform background tasks simultaneously, improving the responsiveness and usability of the application.
Overall, concurrency is a powerful tool for improving the performance and efficiency of Python programs, and it can be used in a wide range of applications and use cases.
12.2. Determining If a Thread Has Started
12.3. Communicating Between Threads
12.4. Locking Critical Sections
12.5. Locking with Deadlock Avoidance
12.6. Storing Thread-Specific State
12.7. Creating a Thread Pool
12.8. Performing Simple Parallel Programming
12.9. Dealing with the GIL (and How to Stop Worrying About It)
12.10. Defining an Actor Task
12.11. Implementing Publish/Subscribe Messaging
12.12. Using Generators As an Alternative to Threads
12.13. Polling Multiple Thread Queues
12.14. Launching a Daemon Process on Unix