Python, create a function to control the process in another file python

Viewed 27

I created these three functions in python (pool.py, funcA.py, config.py). My intent is that having two python files I would like the two functions to interact. Specifically, I have the pool.py file which has a while loop inside that simulates an always active process that should be blocked when the funcA.py file is called separately. The config.py file is used to create a global variable and use this variable in both files, with funcA.py I go to modify these variables cmd0 and cmd1 and instead with the file pool.py I repeatedly read these variables and check if they change. If you want to test the operation you can take these files and open two terminals in one run pool.py and in the other, you can run the funcA.py file then you can change the content of the cmd0 and cmd1 variables in the funcA.py file while the pool.py function is still running on the other terminal and you can see that the output of the pool.py file updates immediately. Now my question is how can I make the contents of the funcA.py file become a function and run it directly as a function and have the same result? That is the one that by launching the new function that is inside the funcA.py file I modify the output of the pool.py function that is running on the other terminal.

config.py

def init():
 global cmd0, cmd1
 cmd0 = ''
 cmd1 = ''
 return cmd0, cmd1

pool.py

 import os
 import time
 import sys
 import numpy as np
 import config
 import funcA
 from importlib import reload

 while True:

   funcA = reload(funcA)
   print(str(config.cmd0))
   print(str(config.cmd1))
   if config.cmd0 == 'start':
    print('stop')
    time.sleep(10)

funcA.py

import os
import sys
import numpy as np
import config

config.cmd0 = 'start'
config.cmd1 = 'u1'
1 Answers

If i understood correctly, the following is working:

pool.py

python
import os
import time
import sys
import numpy as np
import config
import funcA
from importlib import reload

while True:

  funcA = reload(funcA)
  funcA.change_values("cmd0", "cmd1")
  print(str(config.cmd0))
  print(str(config.cmd1))
  if config.cmd0 == 'start':
     print('stop')
     time.sleep(10)

funcA.py

import os
import sys
import numpy as np
import config

config.cmd0 = 'start'
config.cmd1 = 'u1'

def change_values(a, b):
    config.cmd0 = a  
    config.cmd1 = b

As you can see, the function change_values is called in the pool.py without any check, then the cmd0 and cmd1 will change immediately.

Related