简体   繁体   中英

How to run multiple scripts from different folders from one parent script

My parent script: D:\\python\\loanrates\\Parent\\parent.py

One of the child scripts: D:\\python\\loanrates\\test\\test.py

This is my parent script which imports and to (attempts) to run test.py :

import sys
import thread

sys.path.append('/python/loanrates/test')

import test

thread.start_new_thread(test)

It imports test.py fine, but i'm having trouble running it using thread.start_new_thread(test)

The test script contains no functions and is just a simple script that saves a .json to test.py's directory, for completeness i'll here is a paste:

import json 
data = 'ello world'
with open( 'D:/python/loanrates/test/it_worked.json', 'w') as f:
    json.dump(data, f)

Eventually I will be running around 15 of child scripts. But as I said i'm having trouble running multiple threads

It is recommended to use the "Threading" module unless you have a very good reason to use the low level "thread" module, the latter of which can detect fear in the keystrokes of the developer. You will have much better luck with Threading.

#parent.py
from threading import Thread
import sys
sys.path.append('python/loanrates/test')
import test

t1 = Thread(target=test.threadTest())
t2 = Thread(target=test.threadTest()) #replace with other children
t1.start()
t2.start()

|

#test.py (the child)
import json,sys

def threadTest():
    #sys.path.append('python/loanrates/test')
    data = 'ello world'
    with open( 'it_worked.json', 'w') as f:
        json.dump(data, f)

Notice you can design threadTest() so that it accepts arguments, passed by parent.py.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM