Skip to content

Instantly share code, notes, and snippets.

@s-m-e
Last active August 22, 2021 09:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-m-e/65197151fd3ddc74eee319252b4a9d6e to your computer and use it in GitHub Desktop.
Save s-m-e/65197151fd3ddc74eee319252b4a9d6e to your computer and use it in GitHub Desktop.
multiprocessing pool test, process-based and thread-based
# -*- 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