-
-
Save miku/9d32607dc394949e5f56bf2066562c79 to your computer and use it in GitHub Desktop.
Python shelve concurrency
This file contains 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
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