Skip to content

Instantly share code, notes, and snippets.

@eclarke
Created June 15, 2012 19:03
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 eclarke/2938199 to your computer and use it in GitHub Desktop.
Save eclarke/2938199 to your computer and use it in GitHub Desktop.
Spawning processes in Python
import sqlite3
import multiprocessing
store_results_sql = """ some sql code here """
select_results_sql = """ some other sql code here """
def find_enriched(**kwargs):
# do some heavy computation task here
results = enrichment_analysis(**kwargs)
# sqlite is not ideal for parallel processing, but with a timeout it should be fine
with sqlite3.connect("results.db", timeout=30) as conn:
conn.execute(store_results_sql, results)
conn.commit
blocks = [chunk_1, chunk_2, ...]
jobs = []
for block in blocks:
p = multiprocessing.Process(target=find_enriched,
args=(dataset, platform, factor, subset,
block, year, uniprot2entrez_map))
jobs.append(p)
p.start()
# wait for them all to finish
[p.join() for p in jobs]
with sqlite3.connect("results.db", timeout=30) as conn:
for result in conn.execute(select_results_sql):
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment