Home > Back-end >  (Multithreading-Python) How I can create a script that run two scripts which I usually run from two
(Multithreading-Python) How I can create a script that run two scripts which I usually run from two

Time:12-07

I have two scripts a.py and b.py, they send data to each other via a local host (mqtt), and they both depend from a configuration file conf.json. I usually execute them in two different terminals,

  • a.py in one terminal
  • b.py in another

and everything it's OK. I am trying right now to create another script c.py which should do the following:

  • for parameter in parameters
  • update config.json
  • execute a.py and b.py "in two different terminals"
  • close a.py, b.py and start again with the new parameters

Now, I am very noob about this, so I tried to use Thread from threading

from threading import Thread

for parameter in parameter 
    #update config.json
    class exp(Thread):
        def __init__(self, name):
            Thread.__init__(self)
            self.name = name
        def run(self):
            if self.name == 0:
               a.runs()
            else:
               b.runs()
    thread1 = exp(0)
    thread1.start()
    thread2 = exp(1)
    thread2.start()

a.py and b.py scripts both end by:

def runs():
    #whatever runs do
if __name__ = 'main':
   runs()

It runs without errors, but it does not work. I am quite sure there should be a nice and standard solution to this problem. Any ideas? Thanks!

CodePudding user response:

You probably want multiprocessing, not the threading library (look up multiprocessing.Process. Another fairly equivalent option is to use subprocess.call to launch the two scripts via shell.

Regarding threads - Keep in mind they are limited due to the Global Interpreter Lock in CPython - which is the prevalent python implementation and the one you probably are using.

CodePudding user response:

you can us qt Threading. Qt has a very powerful library exactly for this purpose.

  • Related