running script multiple times simultaniously in python 2.7

后端 未结 3 1419
长发绾君心
长发绾君心 2021-01-24 04:44

Hello I am trying to run a script multiple times but would like this to take place at the same time from what I understood i was to use subprocess and threading together howev

3条回答
  •  梦谈多话
    2021-01-24 05:32

    A little elaboration mgilson's answer:

    Let's say we have a folder example1.
    Inside example1 we have two python scripts:
    execution.py, and main.py

    The contents of execution.py looks like this:

    import subprocess
    
    
    def run_main_with_args(filename,name,today,dbfolder):
        print('{} {} {}'.format('\nfilename: ',filename, ''))
        print('{} {} {}'.format('name: ',name, ''))
        print('{} {} {}'.format('today: ',today, ''))
        print('{} {} {}'.format('dbfolder: ',dbfolder, ''))
    
        outfile = dbfolder+ '/' + name + '.txt'
        with open (outfile, 'w') as fout:
            print('name', file=fout)
    

    Also, the contents of main.py look like this:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    #
    # Author      : Bhishan Poudel; Physics Graduate Student, Ohio University
    # Date        : Aug 29, 2016
    #
    
    # Imports
    import multiprocessing,os,subprocess
    import datetime
    import execution  # file: execution.py
    
    #assume we have a function:
    #exection.run_main_with_args(filename,name,today_str,dbfolder)
    
    today = datetime.datetime.today()
    def my_execute(filename):
        if '.txt' in filename:
           name = filename.strip('.txt')
           dbfolder = "db/" + name
           if not os.path.exists(dbfolder): os.makedirs(dbfolder)
           execution.run_main_with_args(filename,name,str(today),dbfolder)
    
    
    
    p = multiprocessing.Pool()
    p.map(my_execute,['file1.txt', 'file2.txt'])
    

    Then, if we run this main.py it will create required files in the required directories in parallel way!

提交回复
热议问题