Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Concurrency in Python

You're reading from   Mastering Concurrency in Python Create faster programs using concurrency, asynchronous, multithreading, and parallel programming

Arrow left icon
Product type Paperback
Published in Nov 2018
Publisher Packt
ISBN-13 9781789343052
Length 446 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Author (1):
Arrow left icon
Quan Nguyen Quan Nguyen
Author Profile Icon Quan Nguyen
Quan Nguyen
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

Preface 1. Advanced Introduction to Concurrent and Parallel Programming FREE CHAPTER 2. Amdahl's Law 3. Working with Threads in Python 4. Using the with Statement in Threads 5. Concurrent Web Requests 6. Working with Processes in Python 7. Reduction Operators in Processes 8. Concurrent Image Processing 9. Introduction to Asynchronous Programming 10. Implementing Asynchronous Programming in Python 11. Building Communication Channels with asyncio 12. Deadlocks 13. Starvation 14. Race Conditions 15. The Global Interpreter Lock 16. Designing Lock-Based and Mutex-Free Concurrent Data Structures 17. Memory Models and Operations on Atomic Types 18. Building a Server from Scratch 19. Testing, Debugging, and Scheduling Concurrent Applications 20. Assessments 21. Other Books You May Enjoy

How to simulate in Python

In this section, we will look at the results of Amdahl's Law through a Python program. Still considering the task of determining whether an integer is a prime number, as discussed in Chapter 1, Advanced Introduction to Concurrent and Parallel Programming, we will see what actual speedup is achieved through concurrency. If you already have the code for the book downloaded from the GitHub page, we are looking at the Chapter02/example1.py file.

As a refresher, the function that checks for prime numbers is as follows:

# Chapter02/example1.py

from math import sqrt

def is_prime(x):
if x < 2:
return False

if x == 2:
return x

if x % 2 == 0:
return False

limit = int(sqrt(x)) + 1
for i in range(3, limit, 2):
if x % i == 0:
return False

return x

The next part of the code is a function that takes in an...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime