简体   繁体   English

我如何循环一个 python 文件以无限期地打开另一个 python 文件?

[英]How would I loop a python file to open another python file indefinitely?

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.所以我知道如何在另一个 Python 文件exec(open('file.py').read())中执行 Python 文件,但是我要循环的文件在文件中有一个while(True):循环。 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.我想要做的是循环打开包含 while true 循环的文件,同时让该文件在后台运行。

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.对此的任何帮助将不胜感激。

Firstly, the exec(...) command is unsafe and really should be avoided.首先, exec(...)命令是不安全的,确实应该避免。 An alternative method would be to import your other Python file as if it were a module.另一种方法是导入您的其他 Python 文件,就好像它是一个模块一样。

To be able to run the second file multiple times in parallel, you could use the threading module built into Python.为了能够多次并行运行第二个文件,您可以使用 Python 中内置的threading模块。

Say we have two files, FileA.py and FileB.py .假设我们有两个文件, FileA.pyFileB.py

FileA.py : FileA.py

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

FileB.py : 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 . FileA.py将启动 5 个不同的线程,每个线程运行来自 FileB.py 的Begin() 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 .您可以替换Begin() function 的内容,重命名它或做任何您喜欢的事情,但一定要更新FileA.py中的线程target

It should be noted that opening too many threads is a bad idea as you may exceed the amount of memory your computer has.应该注意的是,打开太多线程是个坏主意,因为您可能会超过计算机拥有的 memory 的数量。 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.除此之外,您应该确保您的线程最终结束并且不是真正的“无限循环”,因为您可能会发现您必须强制关闭您的FileA.py版本才能结束线程。

For more detail on the threading module you can see the python documentation: https://docs.python.org/3/library/threading.html有关线程模块的更多详细信息,您可以查看 python 文档: https://docs.python.org/3/library/threadingCD67FC35FDC3825D

Or you could fine many guides related to the topic by searching for Python threading on Google.或者,您可以通过在 Google 上搜索 Python threading 来完善与该主题相关的许多指南。

I hope this helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM