Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pausing a Script
#1
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?
Reply
#2
Not too sure, sorry but you can see if this helps.
https://docs.python.org/3/library/time.html
Reply
#3
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".
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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.
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pausing and returning to function? wallgraffiti 1 2,237 Apr-29-2021, 05:30 PM
Last Post: bowlofred
  Slide show with mouse click pausing aantono 1 2,305 Jan-28-2020, 04:25 AM
Last Post: Larz60+
  Pausing a running process? MuntyScruntfundle 2 7,527 Nov-14-2018, 05:14 PM
Last Post: woooee
  Pausing a loop with spacebar, resume again with spacebard bigmit37 10 20,368 May-30-2017, 10:28 AM
Last Post: snippsat
  Pausing and playing a while loop. traceon 1 3,983 Jan-24-2017, 09:11 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020