[Tutor] Running multiple python scripts from one python scripts

ose micah osaosemwe at yahoo.com
Fri Oct 25 09:48:55 EDT 2019


 Hello Alan, 
Here is my use case. I have a master scripts, that dynamically creates python scripts on the fly (from live feeds), at the end of the main script (the master script), This newly created scripts, must be deployed in less than 2 mins max, but, cannot be deployed simultaneously, cos the function each does could affect the other, as a result, they must be deployed sequentially
hence here is my use case.
import fileinputimport osimport sysimport timeimport subprocess as sp......# create script1with open (/tmp/delete_line3.py, 'w+' ) ......# create script2with open (/tmp/create_20lines.py, 'w+' ) ..........# create script3with open (/tmp/delete_ON2_.py, 'w+' ) ..........# create script4with open (/tmp/create_50.py, 'w+' ) ......# do  some other task...........# update memory ...........# running the newly created scripts in processesextProc1 = sp.Popen([sys.executable, '/tmp/delete_line3.py'],  stdout=sp.PIPE)time.sleep(5)extProc2 = sp.Popen([ sys.executable, '/tmp/create_20lines.py'], stdout=sp.PIPE)time.sleep(5)extProc3 = sp.Popen([sys.executable,'/tmp/ delete_ON2_.py'], stdout=sp.PIPE)time.sleep(5)
extProc = sp.Popen([sys.executable,'/tmp/ create_50.py'], stdout=sp.PIPE)time.sleep(5)

# closing the parent version of the pipesextProc1.close()extProc2.close()
extProc3.close()
extProc4.close()

# Waiting for the program to completeextProc1.wait()extProc2.wait()
extProc3.wait()
extProc4.wait()



    On Friday, October 25, 2019, 06:12:45 AM EDT, Alan Gauld <alan.gauld at yahoo.co.uk> wrote:  
 
 
On 25 October 2019, at 07:46, ose micah via Tutor <tutor at python.org> wrote:

>That was my very first deployment. Calling the scripts and running them. But, that would not work in my use case. As some of the scripts are been formed in the main scripts.

Are you saying that you have a python program somewhere that is generating python code and you are then, in this program, running those dynamically created scripts?

If so, there is almost certainly a better way of doing things by creating permanent scripts and passing parameters and data to them.

Perhaps if we knew more about these script creation programs we could offer a cleaner solution.

Executing dynamically created code via os.system or subprocess is a big security hole but, if it's python code, you might as well use the exec function which is just as big a hole, but more efficient!

 One of the biggest issues with this kind of approach is the invisibility of any output, you need to rely on shared knowledge of output files or data tables. That implies a lot of checking to make sure that they exist before every access and that they are new versions and you are not reprocessing old data. It all gets very messy in any kind of real world environment.

Alan g.
  


More information about the Tutor mailing list