2013-08-17 203 views
1

通過Ubuntu系統上的命令行(/ bin/sh),我執行了一個使用multiprocessing.Process()啓動另一個Python3腳本的Python3腳本。我收到以下錯誤消息:從另一個腳本啓動Python腳本 - 奇怪行爲

[email protected]:/media/collier/BETABOTS/Neobot$ ./Betabot #THE SECOND SCRIPT NEVER EXECUTES 
/bin/sh: 1: Syntax error: "(" unexpected (expecting "}") 
Traceback (most recent call last): 
    File "./Betabot", line 26, in <module> 
    JOB_CONFIG = multiprocessing.Process(os.system('./conf/set_data.py3')) 
    File "/usr/lib/python3.3/multiprocessing/process.py", line 72, in __init__ 
    assert group is None, 'group argument must be None for now' 
AssertionError: group argument must be None for now 

#TESTING THE SECOND SCRIPT BY ITSELF IN TWO WAYS (both work) 
[email protected]:/media/collier/BETABOTS/Neobot$ python3 -c "import os; os.system('./conf/set_data.py3')" #WORKS 
[email protected]:/media/collier/BETABOTS/Neobot$ ./conf/set_data.py3 #WORKS 

問題是 - 爲什麼這不起作用?它應該啓動第二個腳本,並繼續執行出問題。

我對試圖解決問題的代碼進行了編輯。 錯誤現在在第13行。同樣的錯誤發生在第12行「JOB_CONFIG = multiprocessing.Process(os.system('date')); JOB_CONFIG.start()」,我用它作爲測試行。我將第12行更改爲「os.system('date')」,並且工作正常,所以錯誤在於多處理命令

#!/usr/bin/env python3 
import os, subprocess, multiprocessing 
def write2file(openfile, WRITE): 
    with open(openfile, 'w') as file: 
     file.write(str(WRITE)) 
writetofile = writefile = filewrite = writer = filewriter = write2file 
global BOTNAME, BOTINIT 
BOTNAME = subprocess.getoutput('cat ./conf/startup.xml | grep -E -i -e \'<property name=\"botname\" value\' | ssed -r -e "s|<property name=\"botname\" value=\"(.*)\"/>|\1|gI"') 
BOTINIT = os.getpid() 

###Setup science information under ./mem/### 
JOB_CONFIG = multiprocessing.Process(os.system('date')); JOB_CONFIG.start() 
JOB_CONFIG = multiprocessing.Process(os.system('./conf/set_data.py3')); JOB_CONFIG.start() 
###START### 
write2file('./mem/BOTINIT_PID', BOTINIT); write2file('./mem/tty', os.ctermid()); write2file('./mem/SERVER_PID', BOTINIT) 
JOB_EMOTION = multiprocessing.Process(os.system('./lib/emoterm -T Emotion -e ./lib/Emotion_System')); JOB_EMOTION.start() 
JOB_SENSORY = multiprocessing.Process(os.system('./lib/Sensory_System')); JOB_SENSORY.start() 
print(BOTNAME + ' is starting'); JOB_CONFIG.join() 
try: 
    os.system('./lib/neoterm -T' + BOTNAME + ' -e ./lib/beta_engine') 
except: 
    print('There seems to be an error.'); JOB_EMOTION.join(); JOB_SENSORY.join(); exit() 
JOB_EMOTION.join(); JOB_SENSORY.join(); exit() 
+1

您不會因爲憤怒而放棄問題而傷害任何人。相反,你應該[改進問題](http://meta.stackexchange.com/a/194077/185951)。 –

+0

你提到你得到一個錯誤。是這樣嗎?什麼是錯誤? –

回答

1

當開始從Python3腳本是在主腳本繼續運行Python3腳本,這樣的命令,必須做到:

JOB_CONFIG = subprocess.Popen([sys.executable, './conf/set_data.py3']) 

的文件名字符串是腳本。這是保存到一個變量,以便我稍後操作該過程。例如,當主腳本應該等待另一個腳本時,我可以使用命令「JOB_CONFIG.wait()」。

至於錯誤消息的第一行中的那個哈希,這是由於使用的第一個子進程命令中的語法錯誤。

+1

['sys.exe.executable'](http://docs.python.org/3/library/sys.html#sys.executable)只是正在執行的當前python二進制文件的路徑,例如'/ usr /斌/ python'。它可以是一個空字符串或'None'。 – jfs

+0

@ J.F.Sebastian,感謝您的糾正。 –