0

如何将此完整export DISPLAY=:0.0 && python /home/src/upload.py %s %s &命令添加到子进程?

# valid command
u ="""export DISPLAY=:0.0 && python /home/src/upload.py %s %s &""" % (s[1], s[2])
d ="""export DISPLAY=:0.0 && python /home/src/download.py %s %s &""" % (s[1], s[2])

# seems to work like this but need to add the u and d on this
subprocess.Popen(["python","/home/src/upload.py"], stdout=subprocess.PIPE)
subprocess.Popen(["python","/home/src/download.py"], stdout=subprocess.PIPE)
4

2 回答 2

0

您可以将DISPLAY变量添加到环境变量中;参数是参数列表的极端元素:

import os

env = os.environ.copy()
env['DISPLAY'] = ':0.0'

p1 = subprocess.Popen(["python","/home/src/upload.py"] + s[1:3], 
                      env=env, stdout=subprocess.PIPE)
p2 = subprocess.Popen(["python","/home/src/download.py"] + s[1:3], 
                      env=env, stdout=subprocess.PIPE)
于 2013-09-26T20:47:15.060 回答
0

你的命令基本上做的是设置一个环境变量,然后用参数调用一个 python 脚本。您可以通过将代码修改为:

import os
import subprocess

new_env = os.environ.copy()
new_env['DISPLAY'] = ':0.0'

subprocess.Popen(["python","/home/src/upload.py", s[1], s[2]], stdout=subprocess.PIPE, env=new_env)
subprocess.Popen(["python","/home/src/download.py", s[1], s[2]], stdout=subprocess.PIPE, env=new_env)
于 2013-09-26T20:49:46.537 回答