<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">"""
Script for running discovery_ae.py in parallel.
"""

import os
import sys
import time
import MySQLdb
import subprocess

max_procs = 6
children = []
# result_file_path = '/Volumes/UsersHD/Users/nick/Stanford/AltmanLab/aers/part1'
result_file_path = os.path.expanduser('~/Stanford/AltmanLab/aers/part2.b')

# script = "src/discover_ae.py"
script = "discover_ae_p2.py"

db = MySQLdb.connect(host="127.0.0.1", port=3307, user="root", passwd="enter_your_password",db="project_aers")
c = db.cursor()

query = """
select distinct stitch_id1
from corr_drug_drug
where `database` = 'aers';
"""
c.execute(query)

drug_cids = [row[0] for row in c.fetchall()]

query = """
select distinct stitch_id
from pred_drug_events_b;
"""
c.execute(query)

already_finished = [row[0] for row in c.fetchall()]

while len(drug_cids) &gt; 0:
    
    while len(children) &lt; max_procs:
        cid = drug_cids.pop()
        
        result_file = "%s/%s.csv" % (result_file_path, cid)
        
        if not os.path.exists(result_file) and not cid in already_finished:
            # Claim this one as in process.
            os.system("touch %s" % result_file)
            child = subprocess.Popen(["/usr/bin/python",script,cid])
            print &gt;&gt; sys.stderr, "Started %s for %s with pid: %s, (%d cids remaining)" % (cid, script, child.pid, len(drug_cids))
            children.append(child)
    
    completed_processes = []
    for child in children:
        if not child.poll() is None:
            completed_processes.append(child)
    
    for child in completed_processes:
        children.remove(child)
    
    # just to keep it from spinning a cpu
    time.sleep(5)

print &gt;&gt; sys.stderr, "Finished discovering adverse events."
os.system("bash ~/bin/email.sh 'finished discovering adverse events in AERS.'")</pre></body></html>