Last active
August 22, 2021 09:41
-
-
Save s-m-e/65197151fd3ddc74eee319252b4a9d6e to your computer and use it in GitHub Desktop.
multiprocessing pool test, process-based and thread-based
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# QGIS 3.x / Python 3.x | |
from math import sqrt, ceil | |
from multiprocessing import Pool, Process, Queue | |
from multiprocessing.pool import ThreadPool | |
WORKERS = 2 | |
def worker_task(number): | |
return sqrt(number ** 2) | |
def test1(): | |
data = list(range(0, 100, 10)) | |
print('creating pool') | |
pool = Pool(WORKERS) | |
print('computing') | |
results = pool.map(worker_task, data) | |
print('result', results) | |
print('closing pool') | |
pool.close() | |
print('done') | |
def test2(): | |
data = list(range(0, 100, 10)) | |
print('creating pool') | |
with Pool(WORKERS) as pool: | |
print('computing') | |
results = pool.map(worker_task, data) | |
print('result', results) | |
print('closing pool') | |
print('done') | |
def worker_wrapper(queue, data): | |
queue.put([worker_task(number) for number in data]) | |
def test3(): | |
data = list(range(0, 100, 10)) | |
interval = ceil(len(data) / WORKERS) | |
print('creating pool') | |
pool = [] | |
for index in range(WORKERS): | |
queue = Queue() | |
process = Process( | |
target = worker_wrapper, | |
args = (queue, data[index*interval:(index+1)*interval]) | |
) | |
process.start() | |
pool.append({'process': process, 'queue': queue}) | |
print('computing') | |
results = [number for worker in pool for number in worker['queue'].get()] | |
print('result', results) | |
print('closing pool') | |
for worker in pool: | |
worker['process'].join() | |
print('done') | |
def test4(): | |
data = list(range(0, 100, 10)) | |
print('creating pool') | |
pool = ThreadPool(WORKERS) | |
print('computing') | |
results = pool.map(worker_task, data) | |
print('result', results) | |
print('closing pool') | |
pool.close() | |
print('done') | |
def test5(): | |
data = list(range(0, 100, 10)) | |
print('creating pool') | |
with ThreadPool(WORKERS) as pool: | |
print('computing') | |
results = pool.map(worker_task, data) | |
print('result', results) | |
print('closing pool') | |
print('done') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment