可以使用subprocess模块或者os模块
有subprocess.Popen()
, subprocess.run()
, os.popen()
, os.system()
示例如下:
main.py
import subprocess
import os
clickType = 'FINGEROPEN'
userID = 123456.234
f = open('log.txt', 'w')
f.write('{0} {1}'.format(clickType, userID))
f.close()
# subprocess.Popen('python sub.py')
# subprocess.Popen(['python', 'sub.py'])
# subprocess.run('python sub.py') # 不是并行执行
# subprocess.run(['python', 'sub.py'])
# os.system('python sub.py') # 不是并行执行
os.popen('python sub.py', 'w')
print('111')
sub.py
f = open('log.txt')
content = f.read()
clickType, userID = content.split()
print(type(userID))
print(clickType, userID)
f.close()
其中,subprocess.run()
和os.system()
创建的子进程不是与主程序并行执行,而是等子进程执行完毕,再继续执行主程序,输出如下:
<class 'str'>
FINGEROPEN 123456.234
111
subprocess.Popen()
和os.popen()
的输出如下:
111
<class 'str'>
FINGEROPEN 123456.234
另外,subprocess.Popen()
在Linux中输入参数貌似只能是序列类型,否则会报错,其他几个命令不知道有没有这个问题
参考:
https://www.runoob.com/w3cnote/python3-subprocess.html
https://www.cnblogs.com/lincappu/p/8270709.html
https://blog.csdn.net/weixin_43895902/article/details/101372770
https://www.runoob.com/python/os-popen.html