Program chaining on Windows
Rob Cliffe
rob.cliffe at btinternet.com
Sun Aug 23 19:49:44 EDT 2020
Thanks for everyone who replied so far, it is appreciated. (I don't
particularly like asking for help and taking up other peoples' time, but
I really ran out of ideas.)
Chris, thanks for your explanation:
With exec, the intention is to*replace* the current program, not to
invoke it and wait till it's done. The current program will not
continue after a successful exec.
That describes exactly what I want to do, but haven't succeeded in doing
so far. It's a bit frustrating that I can't do what I have been doing
routinely for decades in another language.
dn asked
What is the definition of "finish" in the first program?
Not sure if have understood <<<(*not* waiting for the second to
finish, as with e.g. os.system)>>>.
My definition of "finish" is that the program exits (sys.exit() and
friends), without waiting for the second program to finish.
So if I were "chaining" say to a .exe file, the Python interpreter would
shut down immediately.
In Chris' words, I want the second program to *replace* the first one.
Barry, thanks for your suggestion:
os.execl('C:\\Python38\\python.exe', 'C:\\Python38\\python.exe', 'X2.py')
but I'm afraid it didn't help. I tried it and got the same behaviour
(Python and Windows alternately grabbing console input lines) as my
"ATTEMPT #8" which used
subprocess.Popen('-c X2.py', executable=sys.executable)
Anything dubious about exec (or whatever) doesn't worry me as this is
development, not live installation.
Let me describe my actual use case. I am developing a large Python
program (in Windows, working in DOS boxes) and I constantly want to
modify it and re-run it. What I have been doing is to make it respond
to a hotkey by calling itself via os.system. The problem is that if I
do this 50 times I am left with 51 instances still running, each one
waiting for the next to finish. That's actually OK, except that when I
finally shut it down, it takes a long time (about 2.5 sec per instance).
I have found a workaround: a small shell program which calls the main
program (wth os.system), then when the latter exits, calls it again (if
required). Starting the main program is slightly slower, but acceptably
so; shutting it down becomes constant time.
But I would still like to be able to do it as I originally planned, if
possible. Not least because I may have other uses for program
"chaining" in future.
Best wishes
Rob Cliffe
On 23/08/2020 21:37, dn via Python-list wrote:
> On 23/08/2020 19:31, Rob Cliffe via Python-list wrote:
>> On WIndows 10, running Python programs in a DOS box, I would like one
>> Python program to chain to another. I.e. the first program to be
>> replaced by the second (*not* waiting for the second to finish, as
>> with e.g. os.system). This doesn't seem a lot to ask, but so far I
>> have been unable to so this. I am using Python 3.8.3. Some attempts
>> so far (may be nonsensical):
>
> What is the definition of "finish" in the first program?
> Not sure if have understood <<<(*not* waiting for the second to
> finish, as with e.g. os.system)>>>.
> In Python, the easiest way to chain two "modules" is to use import.
> This gives full control to the 'import-er'.
>
>
>> ATTEMPT #1
>> ----------------
>> # File X1.py
>> import os
>> print("This is X1")
>> os.execl('C:\\Python38\\python.exe', 'X2.py')
>>
>> # File X2.py
>> print("This is X2")
>
>
> # File X1.py
> import os
> def fn():
> print("This is X1")
> os.execl('C:\\Python38\\python.exe', 'X2.py')
> # !!!!!
>
> # File X2.py
> def fn():
> print("This is X2")
>
> # File x3.py
> import x1
> import x2
> x1.fn()
> x2.fn()
> print( "x3 terminating" )
>
>
More information about the Python-list
mailing list