running script multiple times simultaniously in python 2.7

后端 未结 3 1447
长发绾君心
长发绾君心 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:35

    Personally, I'd use multiprocessing. I'd write a function that takes a filename and does whatever the main guts of execution does (probably by importing execution and running some function within it):

    import multiprocessing
    import execution
    import datetime
    
    #assume we have a function:
    #exection.run_main_with_args(filename,name,today_str,dbfolder)
    
    today = datetime.datetime.today()
    def my_execute(filename):
        if '.htm' in filename:
           name = filename.strip('.htm')
           dbfolder = "C:/newscript/db/" + name
           os.makedirs(dbfolder)
           execution.run_main_with_args(filename,name,str(today),dbfolder)
    
    p = multiprocessing.Pool()
    p.map(my_execute,list_of_files_to_process)
    

提交回复
热议问题