David Pham David Pham - 7 months ago 15
Python Question

Call function from a file B in a loop in file A

I have a python file B with all my function and a main code which is in loop of 0.25 sec, and I want to call this file in a loop in my file A. Can you get my weird mind ?
What I did but only read the loop from file B once :

while 1:
from FileB import *

And my file B :

while t<0.25:
#my stuff


PS : I forget to mention that i can't modify the file B.


The import statement only reads the target module one time.

If you have control of both files, I'd suggest that you make your loop a function in file B:

def main():
    while t<0.25:
        #my stuff

if __name__ == '__main__':

Then you can call it repeatedly from file A:

from fileB import main as Bmain

while 1:

If you don't have control of the source code for the files (meaning: if the code comes from someone else), there are a few options. Probably the easiest and fastest to code would be to use the os.system(command) function to run the contents of fileB in a separate process.