Skip to main content
 首页 » 编程设计

python之使用结构运行子进程时出现 EOFError

2025年05月04日11www_RR

当我尝试 chown 一个目录时,Fabric 给我一个 EOFError。只有当我从 fabric 之前运行的 python 脚本运行子进程时才会发生。

来 self 的 fabfile:

... 
with settings(warn_only=True): 
    run('python my_scripts/import_local.py') 
sudo('chown www-data:www-data /opt/mm/my_project/uploads -R') 

如果我在 my_scripts/import_local.py 中注释下面的行,我不会收到任何错误:

    p = subprocess.Popen(command.split(), stdout=subprocess.PIPE, 
                         stderr=subprocess.PIPE) 
    result = p.communicate()[0].splitlines() 
    if result: 
        print result 

在保留子流程行的情况下,我得到了这个回溯(使用 --show=debug 运行):

[xx.xx.xxx.xxx] sudo: sudo -S -p 'sudo password:'  /bin/bash -l -c "cd /opt/mm/my_project/current && source environment.sh && chown www-data:www-data /opt/mm/my_project/uploads -R" 
Traceback (most recent call last): 
  File "/Users/jesse.aldridge/.virtualenvs/my_project/lib/python2.6/site-packages/fabric/main.py", line 682, in main 
    *args, **kwargs 
  File "/Users/jesse.aldridge/.virtualenvs/my_project/lib/python2.6/site-packages/fabric/tasks.py", line 232, in execute 
    task.run(*args, **new_kwargs) 
  File "/Users/jesse.aldridge/.virtualenvs/my_project/lib/python2.6/site-packages/fabric/tasks.py", line 105, in run 
    return self.wrapped(*args, **kwargs) 
  File "/Users/jesse.aldridge/Dropbox/my_code/my_project/fabfile.py", line 162, in do_reimport 
    sudo('chown www-data:www-data /opt/mm/my_project/uploads -R') 
  File "/Users/jesse.aldridge/.virtualenvs/my_project/lib/python2.6/site-packages/fabric/network.py", line 343, in host_prompting_wrapper 
    return func(*args, **kwargs) 
  File "/Users/jesse.aldridge/.virtualenvs/my_project/lib/python2.6/site-packages/fabric/operations.py", line 976, in sudo 
    user=user) 
  File "/Users/jesse.aldridge/.virtualenvs/my_project/lib/python2.6/site-packages/fabric/operations.py", line 867, in _run_command 
    combine_stderr) 
  File "/Users/jesse.aldridge/.virtualenvs/my_project/lib/python2.6/site-packages/fabric/operations.py", line 753, in _execute 
    channel.get_pty(width=cols, height=rows) 
  File "/Users/jesse.aldridge/.virtualenvs/my_project/lib/python2.6/site-packages/ssh/channel.py", line 158, in get_pty 
    self._wait_for_event() 
  File "/Users/jesse.aldridge/.virtualenvs/my_project/lib/python2.6/site-packages/ssh/channel.py", line 1084, in _wait_for_event 
    raise e 
EOFError 
Disconnecting from xx.xx.xxx.xxx... done. 

请您参考如下方法:

运行时会发生什么:

$ ssh xx.xx.xxx.xxx -t'sudo -S -p "sudo password:"  /bin/bash -l -c "cd /opt/mm/my_project/current && source environment.sh && chown www-data:www-data /opt/mm/my_project/uploads -R"'