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
Advanced Python Programming

You're reading from   Advanced Python Programming Build high performance, concurrent, and multi-threaded apps with Python using proven design patterns

Arrow left icon
Product type Course
Published in Feb 2019
Publisher Packt
ISBN-13 9781838551216
Length 672 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
Quan Nguyen Quan Nguyen
Author Profile Icon Quan Nguyen
Quan Nguyen
Sakis Kasampalis Sakis Kasampalis
Author Profile Icon Sakis Kasampalis
Sakis Kasampalis
Dr. Gabriele Lanaro Dr. Gabriele Lanaro
Author Profile Icon Dr. Gabriele Lanaro
Dr. Gabriele Lanaro
Arrow right icon
View More author details
Toc

Table of Contents (41) Chapters Close

Title Page
Copyright
About Packt
Contributors
Preface
Benchmarking and Profiling FREE CHAPTER Pure Python Optimizations Fast Array Operations with NumPy and Pandas C Performance with Cython Exploring Compilers Implementing Concurrency Parallel Processing Advanced Introduction to Concurrent and Parallel Programming Amdahl's Law Working with Threads in Python Using the with Statement in Threads Concurrent Web Requests Working with Processes in Python Reduction Operators in Processes Concurrent Image Processing Introduction to Asynchronous Programming Implementing Asynchronous Programming in Python Building Communication Channels with asyncio Deadlocks Starvation Race Conditions The Global Interpreter Lock The Factory Pattern The Builder Pattern Other Creational Patterns The Adapter Pattern The Decorator Pattern The Bridge Pattern The Facade Pattern Other Structural Patterns The Chain of Responsibility Pattern The Command Pattern The Observer Pattern 1. Appendix 2. Other Books You May Enjoy Index

Chapter 21


What is a critical section?

Critical sections indicate shared resources that are accessed by multiple processes or threads in a concurrent application, which can lead to unexpected, and even erroneous, behaviors.

What is a race condition, and why is it undesirable in a concurrent program?

A race condition occurs when two or more threads/processes access and alter a shared resource simultaneously, resulting in mishandled and corrupted data.

What is the underlying cause of a race condition?

The root cause of a race condition is multiple threads/process reading in and altering a shared resource simultaneously; and, when all of the threads/processes finish their execution, only the result of the last thread/process is registered.

How can locks solve the problem of a race condition?

Since the race conditions arise when multiple threads or processes access and write to a shared resource simultaneously, the solution is to isolate the execution of different threads/processes, especially when interacting with the shared resource. With locks, we can turn a shared resource in a concurrent program into a critical section, whose integrity of data is guaranteed to be protected.

Why are locks sometimes undesirable in a concurrent program?

There are a number of disadvantages to using locks: with enough locks implemented in a concurrent program, the whole program might become entirely sequential; locks don't lock anything.

What are the problems race conditions raise in real-life systems and applications?

The problems race conditions raise in real-life systems and applications are as follows:

  • Security: A race condition can be both exploited as a security vulnerability (to give external agents illegal access to a system) and used as random key generation, for security processes.
  • Operating systems: A race condition occurring when two agents (users and applications) interact with the same memory space can lead to unpredictable behaviors.
  • Networking: In networking, a race condition can lead to giving multiple users powerful privileges in a network.
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