2012-11-20 27 views
1

背景:
*我創建使用的Tkinter(便攜式PYscripter,蟒蛇v2.7.3)
*這個程序將充當一個批次的模擬工作選擇器+調度前端,一個商業方程求解程序
*程序需要允許用戶選擇一堆文件來模擬,順序,一前一後。
*它還需要有設施從現有的/正在運行的任務列表修改(添加/刪除)工作。
*每個模擬肯定會幾個小時運行。
*模擬輸出將在單獨的程序來查看和我不需要任何管道輸出。當需要時,將從GUI調用外部查看器。
按順序使用運行Tkinter的和Python外部獨立的進程

***我有一個主界面窗口,它允許用戶:
選擇作業文件,提交作業,查看日誌提交,停止正在運行的任務(一個接一個)
上述效果很好。

問題:
*如果我使用subprocess.Popen(「命令」):所有的模擬輸入文件在同一時間推出。它必須是順序的(由於許可證和內存限制)

*如果我使用subprocess.call(「」)或wait()方法,則GUI掛起,並且沒有停止/添加/修改的範圍工作清單。即使「作業提交」命令位於獨立窗口上,父窗口也會一直掛起,直到作業完成。

問題1:
*我如何按順序啓動仿真作業(如subprocess.call),並允許主界面窗口的工作列表修改或停止作業的目的起作用?
的工作是在一個列表中,使用「askopenfilenames」拍攝,然後運行使用For循環。

守則的相關部分:

cfx5solvepath=r"c:\XXXX" 
def file_chooser_default(): 
    global flist1 
    flist1=askopenfilename(parent = root2, filetypes =[('.def', '*.def'),('All', '*.*'),('.res', '*.res')], title ="Select Simulation run files...", multiple = True)[1:-1].split('} {') 

def ext_process(): 
    o=list(flist1) 
    p=list(flist1) 
    q=list(flist1) 
    i=0 
    while i < len(flist1): 
     p[i]='"%s" -def "%s"'%(cfx5solvepath,flist1[i]) 
     i+=1 
    i=0 
    while i < len(p): 
     q[i]=subprocess.call(p[i]) 
     i+=1 

root2 = Tk() 
root2.minsize(300,300) 
root2.geometry("500x300") 
root2.title("NEW WINDOW") 
frame21=Frame(root2, borderwidth=3, relief="solid").pack() 
w21= Button(root2,fg="blue", text="Choose files to submit",command=file_chooser_default).pack() 
w2a1=Button(root2,fg="white", text= 'Display chosen file names and order', command=lambda:print_var(flist1)).pack() 
w2b1= Button (root2,fg="white", bg="red", text="S U B M I T", command=ext_process).pack() 
root2.mainloop() 

請讓我知道如果你需要任何東西。期待你的幫助。

* 編輯*
關於將通過@Tim建議的修改,該GUI是無牽無掛。由於與主解算器程序相關聯的特定子程序可以停止作業,因此我可以使用正確的命令停止作業。

一旦當前正在運行的作業停止,列表中的下一個作業啓動,自動,因爲我希望。

這是用於停止作業的代碼:

def stop_select(): #Choose the currently running files which are to be stopped 
    global flist3 
    flist3=askdirectory().split('} {') 

def sim_stop(): #STOP the chosen simulation 
    st=list(flist3) 
    os.chdir("%s"%flist3[0]) 
    st= subprocess.call('"%s" -directory "%s"'%(defcfx5stoppath,flist3[0])) 
    ret1=tkMessageBox.showinfo("INFO","Chosen simulation stopped successfully") 
    os.chdir("%s" %currentwd) 



問題2: *一旦上述工作完成後,使用start_new_thread,GUI不響應。當作業在後臺運行時,GUI工作。但start_new_thread文檔說該線程應該在函數返回時靜默地退出。

*此外,我有一個HTML日誌文件,在每個作業完成時寫入/更新。當我使用start_new_thread時,日誌文件內容僅在所有作業完成後纔可見。然而,內容和時間戳是正確的。在不使用start_new_thread的情況下,我能夠刷新HTML文件以獲取更新的提交日誌。

***使用任務管理器退出GUI程序幾次,我突然無法使用start_new_thread函數!我曾嘗試重新安裝PYscripter並重新啓動計算機。我無法找出任何明顯的回溯,這是:

Traceback (most recent call last): 
File "<string>", line 532, in write 
File "C:\Portable Python 2.7.3.1\App\lib\site-packages\rpyc\core\protocol.py", line 439, in _async_request 
seq = self._send_request(handler, args) 
File "C:\Portable Python 2.7.3.1\App\lib\site-packages\rpyc\core\protocol.py", line 229, in _send_request 
self._send(consts.MSG_REQUEST, seq, (handler, self._box(args))) 
File "C:\Portable Python 2.7.3.1\App\lib\site-packages\rpyc\core\protocol.py", line 244, in _box 
if brine.dumpable(obj): 
File "C:\Portable Python 2.7.3.1\App\lib\site-packages\rpyc\core\brine.py", line 369, in dumpable 
return all(dumpable(item) for item in obj) 
File "C:\Portable Python 2.7.3.1\App\lib\site-packages\rpyc\core\brine.py", line 369, in <genexpr> 
return all(dumpable(item) for item in obj) 
File "C:\Portable Python 2.7.3.1\App\lib\site-packages\rpyc\core\brine.py", line 369, in dumpable 
return all(dumpable(item) for item in obj) 
File "C:\Portable Python 2.7.3.1\App\lib\site-packages\rpyc\core\brine.py", line 369, in <genexpr> 
return all(dumpable(item) for item in obj) 
File "C:\Portable Python 2.7.3.1\App\Python_Working_folder\v350.py", line 138, in ext_process 
q[i]=subprocess.call(p[i]) 
File "C:\Portable Python 2.7.3.1\App\lib\subprocess.py", line 493, in call 
return Popen(*popenargs, **kwargs).wait() 
File "C:\Portable Python 2.7.3.1\App\lib\subprocess.py", line 679, in __init__ 
errread, errwrite) 
File "C:\Portable Python 2.7.3.1\App\lib\subprocess.py", line 896, in _execute_child 
startupinfo) 
WindowsError: [Error 2] The system cannot find the file specified 

回答

0

我建議使用一個單獨的線程的工作啓動。最簡單的方法是使用thread模塊中的start_new_thread方法。

更改提交按鈕的命令command=lambda:thread.start_new_thread(ext_process,())

你可能會想它的點擊時禁用按鈕,使它發射完成時。這可以在ext_process內完成。

如果您想允許用戶取消作業,它會變得更加複雜。這個解決方案不會處理。

+0

謝謝@Tim。我編輯了這篇文章。 – Shreyas

+0

@Shreyas:沒問題。還有什麼你需要回答嗎?如果不是,並且您對此答案感到滿意,請[接受它](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。 – Tim

+0

:我剛剛注意到,一旦過程完成,GUI程序就會掛起! – Shreyas

相關問題