Skip to content

Instantly share code, notes, and snippets.

Created November 8, 2019 00:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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
return commands
def run_do_file(
script_name = '',
keeplog = False):
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 = ''
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)
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