2013-03-25 13 views
0

喜然後打開頁面我想雷神啓動服務器 - 傑基爾/ Python的/ PHP等,然後打開瀏覽器托爾開始傑奇在瀏覽器中

但是起點是阻塞的任務。

有沒有辦法在托爾創建一個子進程;或產生一個新的終端窗口 - 無法看到和谷歌給了我沒有合理的答案。

我的代碼

## 
# Project Thor File 
# 
# @use thor list 
## 
class IanWarner < Thor 

## 
# Open Jekyll Server 
# 
# @use thor ian_warner:openServer 
## 
desc "openServer", "Start the Jekyll Server" 
def openServer 

    system("clear") 
    say("\n\t") 

    say("Start Server\n\t") 
    system("jekyll --server 4000 --auto") 

    say("Open Site\n\t") 
    system("open http://localhost:4000") 

    say("\n") 

end 

end 

回答

1

它看起來像你搞亂的東西了。 Thor通常是一個功能強大的CLI包裝器。 CLI本身通常是單線程的。

你有兩個選擇:要麼以創建不同Thor後代並運行它們不同的線程/進程,迫使open線程/進程要等到jekyll start運行(首選)或system("jekyll --server 4000 --auto &")破解(注意:在一個符號。完)

後者將工作,但你仍然是控制服務器啓動(可能需要的時間顯著量)的第二個醜陋的黑客攻擊,實現這一目標是依靠sleep

say("Start Server\n\t") 
system("jekyll --server 4000 --auto &") 

say("Wait for Server\n\t") 
system("sleep 3") 

say("Open Site\n\t") 
system("open http://localhost:4000") 

Upd:很難想象你想要產生什麼。如果你想離開你的化身服務器運行腳本完成後:

desc "openServer", "Start the Jekyll Server" 
    def openServer 
    system "clear" 
    say "\n\t" 

    say "Starting Server…\n\t" 
    r, w = IO.pipe 
    # Jekyll will print it’s running status to STDERR 
    pid = Process.spawn("jekyll --server 4000 --auto", :err=>w) 
    w.close 
    say "Spawned with pid=#{pid}" 
    rr = '' 
    while (rr += r.sysread(1024)) do 
    break if rr.include?('WEBrick::HTTPServer#start') 
    end 
    Process.detach(pid) # !!! Leave the jekyll running 

    say "Open Site\n\t" 
    system "open http://localhost:4000" 
    end 

如果你想關閉該頁面打開後哲基爾,你要調用產卵到open以及和Process.waitpid它。

+0

非常感謝 - 你有第一種方法的例子嗎?現在執行第二個工作 - 但會隨着網站的擴展而不一致。 – 2013-03-25 06:09:05

+0

更新了答案。 – mudasobwa 2013-03-25 07:54:26

+0

是的,我想離開Jekyll服務器運行並理想地輸出到終端。即 再生:1個文件改變 – 2013-03-26 09:16:26