简体   繁体   English

Python 子进程不同的方法

[英]Python subprocess different approaches

I have the following structure我有以下结构

- folder1
-- script1.py
- folder2
-- script2_1.py
-- script2_2.py
- folder2
-- script3.py

script2_1.py has the following line in it script2_1.py 中包含以下行

os.system("python3 script2_2.py "+str(id))

If I SSH into the instance, cd to folder2 and then run script2_1.py如果我 SSH 进入实例,cd到文件夹2然后运行script2_1.py

python3 script2_1.py

it works fine and script2_2.py is called as expected.它工作正常并且 script2_2.py 按预期调用。


Now in script1.py I want to call both script2_2.py and script3.py.现在在 script1.py 中,我想同时调用 script2_2.py 和 script3.py。 I've tested different approaches but no success yet.我测试了不同的方法,但还没有成功。

Approach 1 :方法1

id = str(id)
subprocess.check_call(["/folder2", "script2_2.py "+id])
subprocess.check_call(["/folder3", "script3.py "+id])

which gives这使

PermissionError: [Errno 13] Permission denied: '/folder2' PermissionError:[Errno 13] 权限被拒绝:'/folder2'

Approach 2:方法二:

id = str(id)
commands = '''
cd /folder2/ 
python3 script2_2.py {} 
cd /folder3/ 
python3 script3.py {} 
'''.format(id, id)
p = subprocess.Popen("/bin/sh", stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = p.communicate(commands.encode('utf-8'))

which gives这使

/bin/sh: python3: not found /bin/sh: python3: 未找到

Approach 3:方法3:

id = str(id)
subprocess.call(['cd /folder2/','python3 script2_2.py {}'.format(id)])
subprocess.call(['cd /folder3/','python3 script3.py {}'.format(id)])

which gives这使

FileNotFoundError: [Errno 2] No such file or directory: 'cd /folder2/': 'cd /folder2/' FileNotFoundError:[Errno 2] 没有这样的文件或目录:'cd /folder2/':'cd /folder2/'

Approach 4 :方法4

In this one I've tested args with both string and list, specifying the full path to the.py file, with and without shell=True, without sys.path and with full path in subprocess.call, ...在这一个中,我用字符串和列表测试了 args,指定了 .py 文件的完整路径,有和没有 shell=True,没有 sys.path 和 subprocess.call 中的完整路径,...

id = str(id)
#import sys
#sys.path.insert(1, '/folder2/')
subprocess.call(['/usr/local/bin/python3','/folder2/script2_2.py {}'.format(id)])
#sys.path.insert(1, '/folder3/')
subprocess.call(['/usr/local/bin/python3','/folder3/script3.py {}'.format(id)])

which gives这使

/usr/local/bin/python3: can't open file '/folder2/script2_2.py 80': [Errno 2] No such file or directory /usr/local/bin/python3: 无法打开文件'/folder2/script2_2.py 80': [Errno 2] 没有这样的文件或目录

/usr/local/bin/python3: can't open file '/folder3/script3.py 80': [Errno 2] No such file or directory /usr/local/bin/python3: 无法打开文件'/folder3/script3.py 80': [Errno 2] 没有这样的文件或目录

Eventually I manage to solve it by using and adaptation of Approach 2 with the full path to python3.最终,我设法通过使用和改编方法 2以及到 python3 的完整路径来解决它。

id = str(id)
commands = '''
cd /folder2/ 
/usr/local/bin/python3 script2_2.py {} 
cd /folder3/ 
/usr/local/bin/python3 script3.py {} 
'''.format(id, id)
p = subprocess.Popen("/bin/sh", stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = p.communicate(commands.encode('utf-8'))

To know which one is I SSH into the instance and ran要知道哪个是我 SSH 进入实例并运行

which python3

which returned返回

/usr/local/bin/python3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM