Book Image

Python Parallel Programming Cookbook

By : Giancarlo Zaccone
Book Image

Python Parallel Programming Cookbook

By: Giancarlo Zaccone

Overview of this book

This book will teach you parallel programming techniques using examples in Python and will help you explore the many ways in which you can write code that allows more than one process to happen at once. Starting with introducing you to the world of parallel computing, it moves on to cover the fundamentals in Python. This is followed by exploring the thread-based parallelism model using the Python threading module by synchronizing threads and using locks, mutex, semaphores queues, GIL, and the thread pool. Next you will be taught about process-based parallelism where you will synchronize processes using message passing along with learning about the performance of MPI Python Modules. You will then go on to learn the asynchronous parallel programming model using the Python asyncio module along with handling exceptions. Moving on, you will discover distributed computing with Python, and learn how to install a broker, use Celery Python Module, and create a worker. You will understand anche Pycsp, the Scoop framework, and disk modules in Python. Further on, you will learnGPU programming withPython using the PyCUDA module along with evaluating performance limitations.
Table of Contents (13 chapters)
Python Parallel Programming Cookbook
About the Author
About the Reviewers

Dealing with Asyncio and Futures

Another key component of the Asyncio module is the Future class. This is very similar to concurrent.futures.Futures, but of course, it is adapted in the main mechanism of Asyncio's event loop. The asyncio.Future class represents a result (but can also be an exception) that is not yet available. It therefore represents an abstraction of something that is yet to be accomplished.

Callbacks that have to process any results are in fact added to the instances of this class.

Getting ready

To manage an object Future in Asyncio, we must declare the following:

import asyncio
future = asyncio.Future()

The basic methods of this class are:

  • cancel(): This cancels the future and schedules callbacks

  • result(): This returns the result that this future represents

  • exception(): This returns the exception that was set on this future

  • add_done_callback(fn): This adds a callback that is to be run when future is executed

  • remove_done_callback(fn): This removes all instances of a callback...