我在同一个目录中有两个python文件。
文件1: main.py
import time
import threading
from subprocess import call
def thread_second():
call(["python", "python_test.py"])
processThread = threading.Thread(target=thread_second) # <- note extra ','
processThread.start()
time.sleep(2)
print ('the file is running in the background')
print('exit main')文件2: secondary.py
import time
import os
try:
file = open("runnnig.tmp","x")
file.close()
except Exception as FileExistsError:
print('file already exists')
print('Secondary file is running')
# do some staff
time.sleep(10)我想要完成的是从secondary.py上运行main.py。只有一个问题,我希望secondary.py完全独立于main.py运行,这样在打开secondary.py之后main.py就会退出。有了这个解决方案,我发现here -- secondary.py开始正常运行,但是main.py挂起,直到secondary.py退出。我怎样才能做到这一点?
PS。任何想知道我想做什么的人
我在Raspberry pi上运行了一个node.js服务器。当向服务器发出请求时,将从服务器调用main.py。secondary.py是一个脚本,它告诉raspberry从传感器开始记录值。secondary.py将运行一个无限循环,直到另一个脚本中断它。这就是为什么我不希望main.py挂起直到secondary.py退出
发布于 2020-07-17 13:36:05
不需要线程处理,使用Popen而不是call意味着main.py不会等待新进程的结束。
import time
from subprocess import Popen
Popen(["python", "python_test.py"])
time.sleep(2)
print ('the file is running in the background')
print('exit main')https://stackoverflow.com/questions/62954417
复制相似问题