From e6fd4e7414967de61ee7ecd2dc5c202b30abc1e9 Mon Sep 17 00:00:00 2001 From: Prashant Date: Fri, 10 Nov 2023 17:19:43 -0600 Subject: [PATCH 1/2] Added parallel execution --- sciunit2/command/exec_/__init__.py | 18 +++++++++++++++--- sciunit2/command/mixin.py | 13 +++++++++++++ sciunit2/core.py | 8 ++++---- sciunit2/libexec/__init__.py | 4 ++-- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/sciunit2/command/exec_/__init__.py b/sciunit2/command/exec_/__init__.py index 6054c0c..a9e02ad 100644 --- a/sciunit2/command/exec_/__init__.py +++ b/sciunit2/command/exec_/__init__.py @@ -6,6 +6,8 @@ from sciunit2.util import path_injection_for import sciunit2.core import sciunit2.workspace +import os +import threading from getopt import getopt from pkg_resources import resource_filename @@ -27,10 +29,20 @@ def run(self, args): raise CommandLineError emgr, repo = sciunit2.workspace.current() with emgr.exclusive(): - rev = emgr.add(args) if optlist: + rev = emgr.add(args) standin_fn = resource_filename(__name__, 'sciunit') sciunit2.core.shell(env=path_injection_for(standin_fn)) + results = self.do_commit('cde-package', rev, emgr, repo) else: - sciunit2.core.capture(args) - return self.do_commit('cde-package', rev, emgr, repo) + # make a new directory for the execution + thread_id = threading.current_thread().ident + directory_name = f"thread_{thread_id}" + os.makedirs(directory_name, exist_ok=True) + + sciunit2.core.capture(args,cwd =directory_name) + results = self.do_commit_parallel(os.path.join(directory_name,'cde-package'), emgr, repo,args) + + os.rmdir(directory_name) + + return results diff --git a/sciunit2/command/mixin.py b/sciunit2/command/mixin.py index 3951a33..1c301de 100644 --- a/sciunit2/command/mixin.py +++ b/sciunit2/command/mixin.py @@ -3,6 +3,7 @@ from sciunit2.util import quoted from sciunit2 import timestamp import sciunit2.workspace +import threading from humanfriendly.terminal.spinners import Spinner @@ -12,6 +13,9 @@ # A spinning animation is displayed as feedback # to the end-user during the entire time. class CommitMixin(object): + + lock = threading.Lock() + def do_commit(self, pkgdir, rev, emgr, repo): with Spinner(label='Committing') as sp: # adds the execution to de-duplication engine @@ -19,6 +23,15 @@ def do_commit(self, pkgdir, rev, emgr, repo): # adds the execution to the database return (repo.location,) + emgr.commit(sz) + def do_commit_parallel(self, pkgdir, emgr, repo, args): + with self.lock : + rev = emgr.add(args) + with Spinner(label='Committing') as sp: + # adds the execution to de-duplication engine + sz = repo.checkin(rev, pkgdir, sp) + # adds the execution to the database + return (repo.location,) + emgr.commit(sz) + def note(self, aList): return "\n[%s %s] %s\n Date: %s\n" % ( sciunit2.workspace.project(aList[0]), diff --git a/sciunit2/core.py b/sciunit2/core.py index 15a05ce..b41b27f 100644 --- a/sciunit2/core.py +++ b/sciunit2/core.py @@ -11,10 +11,10 @@ # capture the execution of commands from cde.log -def capture(args): - sciunit2.libexec.ptu(args).wait() - assert os.path.isdir('cde-package') - with open('cde-package/cde.log', 'r+') as f: +def capture(args, cwd = None): + sciunit2.libexec.ptu(args,cwd=cwd).wait() + assert os.path.isdir(os.path.join(cwd,'cde-package')) + with open(os.path.join(cwd,'cde-package/cde.log'), 'r+') as f: f.prepend_cmd(args) diff --git a/sciunit2/libexec/__init__.py b/sciunit2/libexec/__init__.py index e43d9a0..3b29bf8 100644 --- a/sciunit2/libexec/__init__.py +++ b/sciunit2/libexec/__init__.py @@ -19,8 +19,8 @@ def __init__(self, name): def which(self): return pathfor(self.name) - def __call__(self, args, **kwargs): - return subprocess.Popen([self.which] + args, **kwargs) + def __call__(self, args,cwd=None, **kwargs): + return subprocess.Popen([self.which] + args,cwd=cwd, **kwargs) ptu = Process('ptu') From e4fca43641ce190b7287cba4bc9033b98325cdfd Mon Sep 17 00:00:00 2001 From: Prashant Date: Sat, 25 Nov 2023 19:23:43 +0530 Subject: [PATCH 2/2] Test Tox --- sciunit2/cli.py | 1 + sciunit2/command/export.py | 3 +++ 2 files changed, 4 insertions(+) diff --git a/sciunit2/cli.py b/sciunit2/cli.py index 82e3427..d31fe3a 100644 --- a/sciunit2/cli.py +++ b/sciunit2/cli.py @@ -62,6 +62,7 @@ def err2(msg1, msg2): def main(): + print("debug_log") try: _main(sys.argv[1:]) except CommandLineError: diff --git a/sciunit2/command/export.py b/sciunit2/command/export.py index 329c7d3..c046ea5 100644 --- a/sciunit2/command/export.py +++ b/sciunit2/command/export.py @@ -26,10 +26,13 @@ def python_paths(log_file, cde_root_dir): 3. python version """ with open(log_file) as f: + print(log_file) + print(cde_root_dir) line = f.readline() found = False pkg_dir_lines = [] while line: + print(line) line = line.strip() # reading python executable path from the # first line after comments in the log file