Skip to content

Instantly share code, notes, and snippets.

@miku
Forked from fnielsen/gist:3904327
Created August 15, 2020 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miku/9d32607dc394949e5f56bf2066562c79 to your computer and use it in GitHub Desktop.
Save miku/9d32607dc394949e5f56bf2066562c79 to your computer and use it in GitHub Desktop.
Python shelve concurrency
import shelve
import multiprocessing
import os
filename = "tmp.shelve"
N = 4
end = 10000
def insert((offset, jump, end, filename)):
d = shelve.open(filename)
for i in range(offset, end, jump):
d[str(i)] = i
if os.path.exists(filename):
os.unlink(filename)
pool = multiprocessing.Pool(processes=N)
pool.map(insert, zip(range(1, N+1), [N]*N, [end]*N, [filename]*N))
d = shelve.open(filename)
sorted(map(int, d.keys()))[:20]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment