Skip to content

Instantly share code, notes, and snippets.

View gorkem2020's full-sized avatar

Gorkem E. gorkem2020

View GitHub Profile
@gorkem2020
gorkem2020 / python_example.py
Created October 31, 2022 09:36 — forked from wshayes/python_example.py
[python multiprocessing example] writing to file from a queue #python #multiprocessing
# https://stackoverflow.com/a/13530258/886938
import multiprocessing as mp
import time
fn = 'c:/temp/temp.txt'
def worker(arg, q):
'''stupidly simulates long running process'''
start = time.clock()
@gorkem2020
gorkem2020 / concurrent.futures-intro.md
Created October 31, 2022 09:36 — forked from mangecoeur/concurrent.futures-intro.md
Easy parallel python with concurrent.futures

Easy parallel python with concurrent.futures

As of version 3.3, python includes the very promising concurrent.futures module, with elegant context managers for running tasks concurrently. Thanks to the simple and consistent interface you can use both threads and processes with minimal effort.

For most CPU bound tasks - anything that is heavy number crunching - you want your program to use all the CPUs in your PC. The simplest way to get a CPU bound task to run in parallel is to use the ProcessPoolExecutor, which will create enough sub-processes to keep all your CPUs busy.

We use the context manager thusly:

with concurrent.futures.ProcessPoolExecutor() as executor: