2015-10-30 32 views
0

我從基準腳本中複製了部分Python腳本,並嘗試在進行修改後執行它。問題是,它一旦啓動就會掛起。Python首次導入時掛起

Python的腳本是:

# !/usr/bin/python 

# ============================= 
# initialize & configure 
# ============================= 
#import shlex 
#import fnmatch 
import os 
import subprocess 
import resource 
import os, sys, cPickle, time, threading, signal 

from errno import EEXIST 
from os.path import join 
from subprocess import Popen 
#from domain import Record 
from threading import Timer 



def measure(commandline): 

# r,w = os.pipe() 
    forkedPid = os.fork() 

    if forkedPid: # read pickled measurements from the pipe 

     print "Parent proc start and the child pid: ", forkedPid 
#  os.close(w); rPipe = os.fdopen(r); r = cPickle.Unpickler(rPipe) 
#  measurements = r.load() 
#  rPipe.close() 
     os.waitpid(forkedPid,0) 
     print "Parent proc end" 
     return "------------" 

    else: 
     # Sample thread will be destroyed when the forked process _exits 
     class Sample(threading.Thread): 

     def __init__(self,program): 
      threading.Thread.__init__(self) 
      self.setDaemon(1) 
      self.timedout = False 
      self.p = program 
      self.maxMem = 0 
      self.childpids = None 
      self.start() 

     def run(self): 
      try:    
       remaining = maxtime 
       delay=0.01 
       print "Start run deman thread: ", remaining, "delay", delay 
       while remaining > 0:         
        res = resource.getrusage(resource.RUSAGE_CHILDREN) 

        time.sleep(delay)      
        remaining -= delay 
        print "\n res: ",res, " and remaining is: ", remaining 

       else: 
        self.timedout = True 
        os.kill(self.p, signal.SIGKILL) 
      except OSError, (e,err): 
       print "Error ", err 


     try: 

     print "Child proc ", commandline 

     # only write pickles to the pipe 
     # os.close(r); wPipe = os.fdopen(w, 'w'); w = cPickle.Pickler(wPipe) 

     start = time.time() 

     # print "commandLine: ", commandline, " remaining: ",remaining 
     # spawn the program in a separate process 
     p = Popen(commandline,stdout=outFile,stderr=errFile,stdin=inFile, shell=True) 


     # start a thread to sample the program's resident memory use 
     t = Sample(program = p.pid) 
     print "Child ......" 
     # wait for program exit status and resource usage 
     rusage = os.wait3(0) 
     print 'rusage: ', rusage 
     elapsed = time.time() - start 

     # m.userSysTime = rusage[2][0] + rusage[2][1] 
     # m.maxMem = t.rusage 
     # m.cpuLoad = "%" 
     # m.elapsed = elapsed 
     print "Child proc end" 

     except KeyboardInterrupt: 
      print "keyBordInterrupt..." 
      os.kill(p.pid, signal.SIGKILL) 

     except ZeroDivisionError, (e,err): 
      print " error ZeroDiv: " 

     except (OSError,ValueError), (e,err): 
      print " error ", e 
     finally: 
      print "Here is finally section. " 
     #w.dump(m) 
     # wPipe.close() 

     # Sample thread will be destroyed when the forked process _exits 
      os._exit(0) 

if __name__ == '__main__': 
    print "Start now...." 
#measure("jruby \/tmp/test.rb") 

當我使用ps -ef | grep MyAccount,我覺得解釋器將掛在第一`進口指令:

MyAccount 16934 16933 0 12:08 pts/19 00:00:00 import os 

/tmp/test.rb是一個在線Ruby腳本(puts "hello"),它不應該引起任何問題,因爲它已被註釋掉。我跑了ps -ef | grep MyAccount三分鐘,這一個總是在那裏。另外,控制檯中沒有任何輸出,我預計會看到Start now....

+0

你怎麼知道它掛在第一次導入? – BlackVegetable

+0

我一直在運行''ps -ef | grep MyAccount'' 3分鐘,這一個總是在那裏.. –

+0

啊,對不起,我沒有意識到你的文件被稱爲'MyAccount'出於某種原因。我的錯! – BlackVegetable

回答

3

這是因爲你的代碼不是解釋爲Python腳本,而是解釋爲Bash腳本。

的第一行應該是

#!/usr/bin/python 

,而不是

# !/usr/bin/python 

您可以跳過此行,只需使用

python <your_script.py> 

,而不是

./<your_script.py> 
012運行程序

我還認爲您錯誤地讀了ps結果。

ps -ef的最後一列是該命令的名稱,明確地說不是該腳本的一行,並且第一個MyAccount是該用戶的名稱。

所以這個ps輸出意味着進程import os掛起。在某些Linux發行版中有一個稱爲導入的應用程序,請參閱man import。如果試圖從shell執行:

$import os 

它只是等待輸入永遠(直到所中斷),而這正是發生在你身上。