Skip to content

Instantly share code, notes, and snippets.

@apoorvalal
Created November 8, 2019 00: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 apoorvalal/c5efada58e374e75611d86f5d49697a3 to your computer and use it in GitHub Desktop.
Save apoorvalal/c5efada58e374e75611d86f5d49697a3 to your computer and use it in GitHub Desktop.
parallelise stata with python
import os, itertools
from subprocess import run
from multiprocessing.dummy import Pool
from functools import partial
def set_globs(stexec,scname):
global script_name, stata_exec
stata_exec = stexec
script_name = scname
def create_command_list(*args):
commands = []
combinations = list(itertools.product(*args))
for combination in combinations:
arguments = " ".join(combination)
command = stata_exec + ' /e /q do ' + script_name + ' ' + arguments
commands.append(command)
return commands
def run_do_file(
script_location,
commands,
script_name = 'caller.do',
nprocesses=2,
keeplog = False):
os.chdir(script_location)
pool = Pool(nprocesses)
for i, returncode in enumerate(pool.imap(partial(run,shell=True),commands)):
print(i, 'command executed, Returned', returncode)
if __name__ == '__main__':
stexec = 'StataMP-64' # short exec because Stata is already in my Path, else wrap in quotes on windows
scname = 'caller.do'
set_globs(stexec,scname)
script_location = r'C:\Users\alal\Desktop\Research\temp'
files = ['all','sub1','sub2']
yvars = ['life_expectancy','income']
xvars = ['1','2','3']
commands = create_command_list(files,yvars,xvars)
print(commands)
run_do_file(script_location, commands, script_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment