我从基准脚本中复制了部分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
时,我发现解释器会挂在第一个`import指令上:
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 :(得分:3)
这是因为您的代码不是解释为Python脚本,而是解释为Bash脚本。
第一行应该是
#!/usr/bin/python
而不是
# !/usr/bin/python
您可以跳过此行,只需使用
运行程序即可python <your_script.py>
而不是
./<your_script.py>
我还认为您错误地阅读了ps
个结果。
ps -ef
的最后一列是命令的名称,绝对不是脚本的一行,第一列是MyAccount
,是用户的名称。
因此,此ps
输出表示进程import os
已挂起。在某些Linux发行版中有一个名为import的应用程序,请参阅man import
。如果您尝试从shell执行它:
$import os
它只是永远等待输入(直到中断),这就是发生在你身上的事情。