Python for Data Science: The Ultimate Step-by-Step Guide to Learn Python In 7 Days & NLP, Data Science from with Python (Master The basics of Data Science and Improve Artificial Intelligence) by Wilson Richard

Python for Data Science: The Ultimate Step-by-Step Guide to Learn Python In 7 Days & NLP, Data Science from with Python (Master The basics of Data Science and Improve Artificial Intelligence) by Wilson Richard

Author:Wilson, Richard [Wilson, Richard]
Language: eng
Format: epub
Published: 2021-03-20T16:00:00+00:00


CHAPTER 7

Using the with statement in threads

The operator within Python sometimes confuses both beginners and experienced Python programmers. This chapter gives in-depth explanations of the main idea behind the operator with acting as a context manager and its application in joint and parallel programming, in particular, regarding the specific use of locks for thread synchronization. This section also provides specific instances of how this operator is most often used to.

This section will cover the following topics:

The very concept of context management and the options that this operator with provides as a kind of context manager, especially in joint and parallel programming.

Actually the operator syntax withand how to efficiently and effectively apply it.

Different ways to use the operator within co-programming.

Technical requirements

Here is a list of prerequisites for this chapter:

Make sure Python 3 is already installed on your computer

Unload the required repository from GitHub

Throughout this chapter, we will work with a subfolder named Chapter04

Check out the following Code in Action videos

Context management

The operator was with the first proposed in Python 2.5 and has been used for quite some time. However, there is still confusion in its use even by experienced Python programmers. This operator with is mainly used as a context manager that properly manages resources, and this is especially important in joint and parallel programming, when resources are shared by various entities in a particular joint or parallel application.

We start with control files

As an experienced Python user, you have probably already seen that this operator with is used to open and read external files from within Python programs. Considering this task at a certain lower level, this operation of opening some external file in Python consumes a certain resource - in this case, some file descriptor - and your operating system will set a certain limit on this resource. This means that there is some upper limit on how many files can be opened at the same time by a separate process running on your system.

Let's quickly look at an example to illustrate this point further. Let's look at the file Chapter04/example1.pythat is shown in the following code:

# Chapter04/example1.py

n_files = 10

files = []

for i in range(n_files):

files.append(open('output1/sample%i.txt' % i, 'w'))

This program is in a hurry, simply creates 10 text files in the appropriate folder output1: sample0.txt, sample1.txt, ..., sample9.txt. What may be more interesting for us is the fact that these files were open inside our cycle for but were not closed - this is a bad programming practice, which will be discussed later. Now, let's say we wanted to reassign the variable n_filesto a larger number — say, 10,000 — as shown in this code:

# Chapter4/example1.py

n_files = 10000

Files = []

# method 1

For I in range (n_files):

Files. Append (open ('output1/sample%i.txt' % I, 'w'))

We will get some error similar to the following:

> Python example1.py

Traceback (most recent call last):

File "example1.py", line 7, in <module>

OSError: [Errno 24] Too many open files: 'output1/sample253.txt'

Taking a closer look at this error message, we can see that my laptop is capable of processing only 253 open files at a



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.