0

So I know how to execute a Python file within another Python file, exec(open('file.py').read()) But the file that I want to loop has a while(True): loop within the file. What I want to do is loop the opening of the file containing the while true loop, while having that file run in the background.

The Opening Code:

loopCount=1
maxCount=100
while(loopcount<=maxCount):
     exec(open('whileTrue.py').read())

I would think that you would do something like this, but instead of opening the file, allowing the file to run in the background, and opening the file again, etc; the code opens the file once then has the file run without opening the next file until after the file has been closed.

Any help with this is greatly appreciated.

Michael Pate
  • 89
  • 1
  • 9
  • If I understand your question I think you cannot do what you like to do or at least that this is not a really good idea. You might try read in the python cde and parse it it to identify the while loop and add code to break out of it, but seriously I don't think you should do this. Perhaps you can try to really explain what you try to achieve and perhaps there is a different solution using `exec(open().read())` works but is not really the standard way of excuting another python file. (import / reload / multiprocessing / subprocess.Popen ore more common ways – gelonida Apr 23 '20 at 00:26

1 Answers1

0

Firstly, the exec(...) command is unsafe and really should be avoided. An alternative method would be to import your other Python file as if it were a module.

To be able to run the second file multiple times in parallel, you could use the threading module built into Python.

Say we have two files, FileA.py and FileB.py.

FileA.py:

import threading, FileB
for i in range(5):
    currentThread = threading.Thread(target=FileB.Begin)
    currentThread.start()

FileB.py:

import time
def Begin():
    for i in range(10):
        print("Currently on the {} second...".format(i))
        time.sleep(1)

FileA.py will start 5 different threads, each running the Begin() function from FileB.py. You may replace the contents of the Begin() function, rename it or do whatever you like, but be sure to update the threads target in FileA.py.

It should be noted that opening too many threads is a bad idea as you may exceed the amount of memory your computer has. As well as that, you should ensure that your threads eventually end and are not a true 'infinite loop', as you may find you have to force your version of FileA.py to close in order to end the threads.

For more detail on the threading module you can see the python documentation: https://docs.python.org/3/library/threading.html

Or you could fine many guides related to the topic by searching for Python threading on Google.

I hope this helps.

Aaron Boult
  • 246
  • 1
  • 7
  • Why is `exec()` unsafe? – Captain Jack Sparrow Apr 23 '20 at 13:02
  • 1
    There are almost always more clear methods to execute code than using `exec()`, and so it is generally considered better to use those clearer methods. As well as that, locating bugs in complex commands passed to `exec()` may be difficult. The `exec()` command also opens up windows to code injection if not properly filtered, and so using alternative methods to accomplish your task would be preferred. A short answer to this can be found here: https://stackoverflow.com/questions/1933451/why-should-exec-and-eval-be-avoided – Aaron Boult Apr 23 '20 at 15:02