Python Forum

Full Version: Pausing a Script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a script/module (call it "main") that calls another script/module (call it "other"). I use runpy.run_path() to execute the call. All's good, except "other" is supposed write to a variable in "main" but "other" takes a few seconds to run. "main" has already executed it's lines by the time "other" is done. So, how do I force "main" to wait until "other" is finished executing and sending over the info to populate the variable in "main"? I know of timer.sleep(), which is fine by me, but I can't figure out how to wait the right amount of time for "other" to finish execution. Here's the code-
"main" (business end only):
mem_dumps = []
runpy.run_path('delete_dump.py')
time.sleep(4)
print(mem_dumps)
sys.exit()
"other":
import os, ctypes, sys, practice_gui

def scan_dumps():
    dumps = []
    for dpath, dname, fname in os.walk('C:\\'):
        for fn in fname:
            if fn.endswith('.dmp'):
                dumps.append(os.path.join(dpath, fn))
    return dumps

if ctypes.windll.shell32.IsUserAnAdmin():
    practice_gui.mem_dumps = scan_dumps()
else:
    ctypes.windll.shell32.ShellExecuteW(None, 'runas', sys.executable, 'delete_dump.py', None, 1)
"other' actually runs twice--once to restart as admin and once as admin. I need to send scan_dumps return to mem_dumps in "main". I think I'm solid, I just don't know how to determine the right amount of time to wait.

-m

EDIT:

It turns out, calling time.sleep() puts "other" to sleep too. How can I work around that?
Not too sure, sorry but you can see if this helps.
https://docs.python.org/3/library/time.html
there is no definite way to know how long to wait. you need to design other to send a message to main ... "i am done".
After I posted my problem I kept working at it and found that using time.sleep() just plain won't work. There may be something with running the script twice; I have to look into it more. I'll post back with what I find for those who may be interested.
Alright. A quarter of a day later (with some breaks) and I've hit pay dirt. Most of my problems were with Windows and it's administrator privileges system. I needed to run my IDE (PyScripter) as admin; all fell into place after that. Code:
"main":(practice_gui.py)
if ctypes.windll.shell32.IsUserAnAdmin():
    mem_dumps = {}
    size = 0
    runpy.run_path('check_dumps.py')
    while not size:
        continue
    ...
"other":(check_dumps.py)
import os, ctypes, sys, practice_gui

def scan_dumps():
    dumps = {}
    k = 0
    for dpath, dname, fname in os.walk('C:\\'):
        for fn in fname:
            if fn.endswith('.dmp'):
                dumps[k] = [os.path.join(dpath, fn), os.stat(os.path.join(dpath, fn)).st_size]
                k += 1
    return dumps

if ctypes.windll.shell32.IsUserAnAdmin():
    dmp_dict = dict(scan_dumps())
    #practice_gui.mem_dumps = dict(dmp_dict)
    i = 0
    tsize = 0
    while i < len(dmp_dict):
        tsize += dmp_dict[i][1]
        i += 1
    tsize /= 1024
    tsize = round(tsize)
    tsize = format(tsize, ',')
    practice_gui.size = tsize
else:
    ctypes.windll.shell32.ShellExecuteW(None, 'runas', sys.executable, 'delete_dump.py', None, 1)
All is working as I'd like with the above code. So that's that.

-m