concurrent.futures

ProcessPoolExecutor from concurrent.futures way slower than multiprocessing.Pool

十年热恋 提交于 2019-11-29 20:26:19
I was experimenting with the new shiny concurrent.futures module introduced in Python 3.2, and I've noticed that, almost with identical code, using the Pool from concurrent.futures is way slower than using multiprocessing.Pool . This is the version using multiprocessing: def hard_work(n): # Real hard work here pass if __name__ == '__main__': from multiprocessing import Pool, cpu_count try: workers = cpu_count() except NotImplementedError: workers = 1 pool = Pool(processes=workers) result = pool.map(hard_work, range(100, 1000000)) And this is using concurrent.futures: def hard_work(n): # Real

Python ThreadPoolExecutor - is the callback guaranteed to run in the same thread as submitted func?

ぃ、小莉子 提交于 2019-11-29 17:34:46
问题 In the ThreadPoolExecutor (TPE), is the callback always guaranteed to run in the same thread as the submitted function? For example, I tested this with the following code. I ran it many times and it seemed like func and callback always ran in the same thread. import concurrent.futures import random import threading import time executor = concurrent.futures.ThreadPoolExecutor(max_workers=3) def func(x): time.sleep(random.random()) return threading.current_thread().name def callback(future):

What's the difference between python's multiprocessing and concurrent.futures?

左心房为你撑大大i 提交于 2019-11-29 17:15:05
问题 A simple way of implementing multiprocessing in python is from multiprocessing import Pool def calculate(number): return number if __name__ == '__main__': pool = Pool() result = pool.map(calculate, range(4)) An alternative implementation based on futures is from concurrent.futures import ProcessPoolExecutor def calculate(number): return number with ProcessPoolExecutor() as executor: result = executor.map(calculate, range(4)) Both alternatives do essentially the same thing, but one striking

How to detect exceptions in concurrent.futures in Python3?

给你一囗甜甜゛ 提交于 2019-11-29 06:49:38
I have just moved on to python3 as a result of its concurrent futures module. I was wondering if I could get it to detect errors. I want to use concurrent futures to parallel program, if there are more efficent modules please let me know. I do not like multiprocessing as it is too complicated and not much documentation is out. It would be great however if someone could write a Hello World without classes only functions using multiprocessing to parallel compute so that it is easy to understand. Here is a simple script: from concurrent.futures import ThreadPoolExecutor def pri(): print("Hello

Is concurrent.futures a medicine of the GIL?

 ̄綄美尐妖づ 提交于 2019-11-29 02:52:20
问题 I was just searching about this new implementation, and i use python 2.7, i must install this, so if i use it, i'll forget the word GIL on CPython? 回答1: No, concurrent.futures has almost nothing whatsoever to do with the GIL. Using processes instead of threads is medicine for the GIL. (Of course, like all medicine, it has side effects. But it works.) The futures module just gives you a simpler way to schedule and wait on tasks than using threading or multiprocessing directly. And it has the

What is the difference between concurrent.futures and asyncio.futures?

♀尐吖头ヾ 提交于 2019-11-29 01:53:24
问题 To clarify the reason for this question: It is confusing to use two modules with the same name. What do they represent that makes them distinct? What task(s) can one solve that the other can't and vice-versa? 回答1: The asyncio documentation covers the differences: class asyncio.Future(*, loop=None) This class is almost compatible with concurrent.futures.Future . Differences: result() and exception() do not take a timeout argument and raise an exception when the future isn’t done yet. Callbacks

python concurrent.futures.ProcessPoolExecutor: Performance of .submit() vs .map()

你说的曾经没有我的故事 提交于 2019-11-29 01:50:01
I am using concurrent.futures.ProcessPoolExecutor to find the occurrence of a number from a number range. The intent is to investigate the amount of speed-up performance gained from concurrency. To benchmark performance, I have a control - a serial code to perform said task (shown below). I have written 2 concurrent codes, one using concurrent.futures.ProcessPoolExecutor.submit() and the other using concurrent.futures.ProcessPoolExecutor.map() to perform the same task. They are shown below. Advice on drafting the former and latter can be seen here and here , respectively. The task issued to

How do you kill Futures once they have started?

给你一囗甜甜゛ 提交于 2019-11-28 19:11:05
I am using the new concurrent.futures module (which also has a Python 2 backport) to do some simple multithreaded I/O. I am having trouble understanding how to cleanly kill tasks started using this module. Check out the following Python 2/3 script, which reproduces the behavior I'm seeing: #!/usr/bin/env python from __future__ import print_function import concurrent.futures import time def control_c_this(): with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: future1 = executor.submit(wait_a_bit, name="Jack") future2 = executor.submit(wait_a_bit, name="Jill") for future in

Multiprocessing Share Unserializable Objects Between Processes

时光总嘲笑我的痴心妄想 提交于 2019-11-27 15:04:51
There are three questions as possible duplicates (but too specific): How to properly set up multiprocessing proxy objects for objects that already exist Share object with process (multiprocess) Can I use a ProcessPoolExecutor from within a Future? By answering this question all three other questions can be answered. Hopefully I make myself clear: Once I created an object in some process created by multiprocessing: How do I pass a reference to that object to an other process? (not so important) How do I make sure that this process does not die while I hold a reference? Example 1 (solved) from

python concurrent.futures.ProcessPoolExecutor: Performance of .submit() vs .map()

不羁的心 提交于 2019-11-27 14:51:00
问题 I am using concurrent.futures.ProcessPoolExecutor to find the occurrence of a number from a number range. The intent is to investigate the amount of speed-up performance gained from concurrency. To benchmark performance, I have a control - a serial code to perform said task (shown below). I have written 2 concurrent codes, one using concurrent.futures.ProcessPoolExecutor.submit() and the other using concurrent.futures.ProcessPoolExecutor.map() to perform the same task. They are shown below.