2017-07-30 113 views
0

所以我有主python腳本,我想從我的主文件調用另一個python腳本,但是,每當我這樣做時,腳本我稱之爲有點超過原來的。有沒有辦法在後臺調用python腳本讓它不會中斷控制檯中的主腳本?如何從另一個python腳本運行python腳本,而不需要第二個腳本中斷第一個腳本?

+1

當然了,瞭解'subprocess'模塊。 –

+0

[Non blocking subprocess.call]的可能重複(https://stackoverflow.com/questions/16071866/non-blocking-subprocess-call) –

+0

也在這裏:https://stackoverflow.com/questions/16986658/python- call-external-program-non-blocking-way –

回答

1

您好我讓這個腳本您使用線程和子進程在後臺運行其它python腳本(不含二級腳本中斷第一)

import threading 
from subprocess import call 
def thread_second(): 
    call(["python", "secondscript.py"]) 
processThread = threading.Thread(target=thread_second) # <- note extra ',' 
processThread.start() 

print 'the file is run in the background' 
+0

這很好,但有什麼辦法可以阻止輔助腳本輸出到控制檯嗎? – invisabuble

+0

@invisabuble這是一個完整的單獨問題,您應該查找或詢問。 LMGTFY:https://stackoverflow.com/questions/2125702/how-to-suppress-console-output-in-python#25061573,https://docs.python.org/3.5/library/subprocess.html#subprocess。調用顯示你有一個kwarg的調用路由你的標準輸出和stderr –

+0

你好,你可以使用它是有幫助的https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a -Thread功能於蟒蛇 – 2017-07-30 20:15:36

相關問題