属性错误: 'module'对象没有属性'kill'



这是我的代码:

def cmdoutput(cmd1, flag):
    finish = time.time() + 50
    p = subprocess.Popen(cmd1, stdin=subprocess.PIPE, stdout = subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
    while p.poll() is None:
        time.sleep(1)
        if finish < time.time():
            os.kill(p.pid, signal.SIGTERM)
            print "timed out and killed child, collecting what output exists so far"
        if (flag == "1"):#To enable container
            out, err = p.communicate(input='containernzone1')
    else:
        out, err = p.communicate()
    print (out)
    return out

当我运行这个脚本时,我得到

Attribute Error: 'module' object has no attribute 'kill'. 

我的代码出了什么问题?

我认为您有自己的os.py

print os.__file__放在os.kill(...)行之前,你就会看到发生了什么


更新

os.kill仅在unix的jython 中可用

使用p.kill()而不是os.kill(...)

更新

p.kill()不工作。(至少在Windows+Jython 2.5.2、2.5.3中)。

p.pid为无。

http://bugs.jython.org/issue1898

使用包装器from pyngrok import ngrok

下面我将分享我自己的工作示例,该示例将MLFlowGoogleColab隧道传输到ngrok上的临时公共服务器。

from pyngrok import ngrok
import mlflow
from getpass import getpass
import logging
# prepare logging
logger = logging.getLogger('My Analysis')
logger.setLevel(logging.INFO)
# Run mlflow server
get_ipython().system_raw("mlflow ui --port 5000 &")
# Terminate open tunnels if exist
ngrok.kill()
# Setting the authtoken (optional)
# Get your authtoken from https://dashboard.ngrok.com/auth
NGROK_AUTH_TOKEN = getpass('Enter the ngrok authtoken: ')  
ngrok.set_auth_token(NGROK_AUTH_TOKEN)
# Open an HTTPS tunnel on port 5000 for http://localhost:5000
ngrok_tunnel = ngrok.connect(addr="5000", proto="http", bind_tls=True)
# visit the tracking address to enable it
mlflow.set_tracking_uri(ngrok_tunnel.public_url)
logger.info(f"MLflow Tracking UI:{ngrok_tunnel.public_url}")

按如下方式更改代码。更改CPYTHON_EXECUTABLE_PATHCMDOUTPUT_SCRIPT_PATH

CPYTHON_EXECUTABLE_PATH = r'c:python27python.exe' # Change path to python.exe
CMDOUTPUT_SCRIPT_PATH = r'c:usersfalsetrucmdoutput.py' # Change path to the script
def cmdoutput(cmd1, flag):
    return subprocess.check_output([CPYTHON_EXECUTABLE_PATH, CMDOUTPUT_SCRIPT_PATH, flag])

将以下代码保存为cmdoutput.py

import subprocess
import sys
def cmdoutput(cmd1, flag):
    finish = time.time() + 50
    p = subprocess.Popen(cmd1, stdin=subprocess.PIPE, stdout = subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
    while p.poll() is None:
        time.sleep(1)
        if finish < time.time():
            p.kill()
            return '<<timeout>>'
        if flag == "1":
            out, err = p.communicate('containernzone1')
    else:
        out, err = p.communicate()
    return out
if __name__ == '__main__':
    cmd, flag = sys.argv[1:3]
    print(cmdoutput(cmd, flag))

最新更新