packtpublishing / mastering-concurrency-in-python Goto Github PK
View Code? Open in Web Editor NEWMastering Concurrency in Python, published by Packt
License: MIT License
Mastering Concurrency in Python, published by Packt
License: MIT License
When running some scripts on Windows, a following RuntimeError is raised:
raise RuntimeError('''
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
...
This can be fixed by introducing the main guard above the first top-level statement that is not a function or an import and indenting everything by one level after that. Here are the locations and potential solutions:
if __name__ == '__main__':
input = [i for i in range(10 ** 13, 10 ** 13 + 500)]
# sequential
...
if __name__ == '__main__':
input = [i for i in range(10 ** 13, 10 ** 13 + 1000)]
for n_workers in range(1, multiprocessing.cpu_count() + 1):
...
if __name__ == '__main__':
my_array = [i for i in range(20)]
result = reduce_sum(my_array)
print('Final result: %i.' % result)
Pycaches are built for specific Python version so it doesn't make much sense to commit them if the end-user will be using a different Python version.
In example 2 of chapter 1. When I run the file I get
Result is very large. Only printing the last 5 digits: 35443
Sequential took: 0.05 seconds.
Result is very large. Only printing the last 5 digits: 45807
Concurrent took: 0.02 seconds.
but 35433 is not equal to 45807. The results should be equal to 35443. What is the issue here ?
Hi There,
I tried out examples 05 and 06 from Chapter08 of the book. Both of them run error-free. However, I don't observer any speed-up in Example-06. In fact example06 is slower. Outputs from both examples are below. I am using a Spyder4.0.1 IDE with Python version 3.7.6 on a windows-10 machine with 12 cores. Would appreciate any pointers on how this issue can be resolved.
============Example05.py output====================
Took 1.2080 seconds with 1 process(es).
Took 1.1229 seconds with 2 process(es).
Took 0.9995 seconds with 3 process(es).
Took 1.0282 seconds with 4 process(es).
Took 1.0535 seconds with 5 process(es).
Took 1.0578 seconds with 6 process(es).
Done.
============Example06.py output====================
Took 1.4141 seconds with 1 process(es).
Took 1.1089 seconds with 2 process(es).
Took 1.0429 seconds with 3 process(es).
Took 1.0382 seconds with 4 process(es).
Took 1.0399 seconds with 5 process(es).
Took 1.0465 seconds with 6 process(es).
Done.
The code in the consumer class doesn't guarantee that although the queue has more than two elements, a single consumer can get all the two element. Because other consumers will compete for these elements too, which will often result in a situation that the two elements are got by two different consumers, therefore making the control variable result_size
work in an unexpected manner.
Add a lock when get element from queue.
lock = multiprocessing.Lock()
class ReductionConsumer(multiprocessing.Process):
def __init__(self, task_queue, result_queue):
...
def run(self):
pname = self.name
print("Using process %s..." % pname)
while True:
with lock:
num1 = self.task_queue.get()
...
self.task_queue.task_done()
self.result_queue.put(num1 + num2)
Package concurrencytest that is imported in Chapter19/example7.py can't be used on Windows, because the default interpreter is missing implementation of the os.fork
function. When running the example under Windows the following error is generated:
AttributeError: module 'os' has no attribute 'fork'
Also the package is missing from software list in README.
When running script Chapter19/example3.py a NameError is raised:
scheduler.shutdown()
NameError: name 'scheduler' is not defined
The problem can be fixed by indenting the line 27 under the main guard.
if __name__ == '__main__':
scheduler = BackgroundScheduler()
...
scheduler.shutdown()
if __name__ == '__main__':
scheduler = BackgroundScheduler()
...
scheduler.shutdown()
When running the file Chapter05/example6.py in Python 3.9 or newer, this error is raised:
AttributeError: 'MyThread' object has no attribute 'isAlive'. Did you mean: 'is_alive'?
This can be fixed by renaming the method at line 21.
alive = [1 if thread.isAlive() else 0 for thread in threads] # original
alive = [1 if thread.is_alive() else 0 for thread in threads] # solution
Also when running the script under Python 3.10 or newer, a warning is emitted:
DeprecationWarning: setDaemon() is deprecated, set the daemon attribute instead
The issue can be fixed by changing the line 41 like so:
thread.setDaemon(True) # original
thread.daemon = True # solution
The method threading.currentThread
was also deprecated in favor of threading.current_thread
. When running scripts Chapter12/example3.py and Chapter12/example4.py a warning will be emitted:
DeprecationWarning: currentThread() is deprecated, use current_thread() instead
Interestingly, in chapter 13, the correct method threading.current_thread
is used.
When running the echo server example from chapter 10 under Python 3.10 or newer a TypeError is raised:
TypeError: BaseEventLoop.create_server() got an unexpected keyword argument 'loop'
The solution is to simply remove the 'loop' argument from line 17.
coro = asyncio.start_server(handle_echo, '127.0.0.1', 8888, loop=loop) # original
coro = asyncio.start_server(handle_echo, '127.0.0.1', 8888) # solution
This can be done safely because the function asyncio.get_event_loop
that is used internally by asyncio.start_server
always returns the currently running loop from Python 3.6 onwards.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.