0

I'm developing a system where I'll need execute many Python scripts at same time. I'm using this code to do it:

import os
from multiprocessing import Pool

processes = ('grabber.py', 'mailer.py', 'updater.py')


def run_process(process):
    os.system('python {}'.format(process))


pool = Pool(processes=3)
pool.map(run_process, processes)

This code is useful and solve my problem, but the script 'updater.py' is responsible to update the other codes 'grabber.py' and 'mailer.py' doing this steps:

1 - Try to download the news scripts (grabber and mailer) with updates from a remote server 2 - Make a Backup of current scripts 3 - Stop grabber.py and mailer.py 4 - Replace the scripts 5 - Reexecute them again

But, I'm thinking about my scenario. I'm using a Pool of processes to run my scripts. What the best way to sinalize my Pool Processes about this update Process and stop mailer and grabber?

Thank you!

  • 1
    If `updater` updates the other scripts, run that first before you run other scripts in a `Pool`. – Rocky Li Dec 04 '18 at 19:17
  • 1
    See this:https://www.tutorialspoint.com/python/python_multithreading.htm – I_Al-thamary Dec 04 '18 at 19:19
  • 1
    And this :https://stackoverflow.com/questions/2846653/how-to-use-threading-in-python – I_Al-thamary Dec 04 '18 at 19:19
  • @RockyLi thank you for your answer! So, the updater needs to be executed at same time, because, the scripts can be updated any time. For example,the programmer can add a new routine in grabber.py, save and put into a remote server. Then, in a system, a support guy will sinalize that a new update will available ( updater will ask for a webservice of new updates are coming) and execute the download of script and to do the steps mentioned above... – julien nascimento Dec 04 '18 at 19:26
  • 1
    I still don't see why they can't be executed sequentially. Given system update, updater runs, after it finishes, everything else runs in a sequential manner, what's the problem? You don't need to go in there and manually run other things, it's still in one script. – Rocky Li Dec 04 '18 at 19:32
  • But, when update execute overwrite the old script for the new script, a overwrite system error can occur? – julien nascimento Dec 04 '18 at 19:36
  • I think it will make it worse if threads are dependent. So , you can see this example how they solve it: https://stackoverflow.com/questions/12620748/running-two-threads-which-are-dependent-on-each-other-in-a-loop – I_Al-thamary Dec 04 '18 at 19:40

1 Answers1

0

You can try this way:

import threading
from time import sleep

threading.Thread(target='updater.py').start()
sleep(0.01)
threading.Thread(target='grabber.py').start()
sleep(0.01)
threading.Thread(target='mailer.py').start()
sleep(0.01)
I_Al-thamary
  • 3,385
  • 2
  • 24
  • 37