Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
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 Pure Python Optimizations FREE CHAPTER 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 11


What is a file descriptor, and in what ways can it be handled in Python?

A file descriptor is used as a handle on an opened external file in a program. In Python, a file descriptor is handled by either using open() and close() functions or using the with statement; for example:

  • f = open(filename, 'r'); ... ; f.close()
  • with open(filename, 'r') as f: ...

What problem arises when file descriptors are not handled carefully?

Systems can only handle a certain number of opened external files in one running process. When that limit is passed, the handles on the opened files will be compromised and file descriptor leakage will occur.

What is a lock, and in what ways can it be handled in Python?

A lock is a mechanism in concurrent and parallel programming that performs thread synchronization. In Python, a threading.Lock object can be handled by either using the acquire() and release() methods or using the with statement; for example:

  • my_lock.acquire(); ... ; my_lock.release()
  • with my_lock: ...

What problem arises when locks are not handled carefully?

When an exception occurs while a lock is acquired, the lock can never be released and acquired again if it is not handled carefully, causing a common problem in concurrent and parallel programming called deadlock.

What is the idea behind context managers?

Context managers are in charge of the context of resources within a program; they define and handle the interaction of other entities with those resources, and perform cleanup tasks after the program exits the context.

What options does thewithstatement in Python provide, in terms of context management?

The with statement in Python offers an intuitive and convenient way to manage resources while ensuring that errors and exceptions are handled correctly. Aside from better error handling and guaranteed cleanup tasks, the with statement also provides extra readability from your programs, which is one of the strongest features that Python offers to its developers.

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 $19.99/month. Cancel anytime
Banner background image