同时运行两个python文件 [英] Run two python files at the same time
问题描述
我尝试使用
#!/bin/bash
python ScriptA.py &
python ScriptB.py &
可以同时运行两个脚本,但是即使所有python文件都位于同一文件夹中,它也始终使用ScriptA返回无效语法".
同时运行两个脚本的文件:
def song():
user = input()
if user == "Chance":
python ScriptA.py &
python ScriptB.py &
else:
print("Error")
到目前为止,我发现的解决方案(例如将该脚本放在一行中)无法正常工作,因为错误仍然显示.
-------------------------- EDIT -------------------- ------
两个脚本都可以单独正常运行,但是,您提供的所有解决方案仍按顺序运行.脚本A是通过OpenCV播放的视频,脚本B是通过playsound播放的歌曲.
ScriptA:
import cv2
import numpy as np
import os
os.environ['SDL_VIDEO_CENTERED'] = '1'
cap = cv2.VideoCapture("video.mp4")
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
cv2.imshow('Frame',frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
ScriptB:
from playsound import playsound
a = (r"C:\Users\A\Desktop\sound.mp3")
playsound(a)
您可能会说,我正在尝试在视频旁显示一首歌曲.我试图显示有声音的视频,但是由于某些原因,openCV无法输出声音.有什么建议?
我认为您正在寻找多线程
您可以将两个脚本合并为另一个脚本, 然后用theads消遣他们
-编辑-
from threading import Thread
import cv2
import numpy as np
import os
from playsound import playsound
def play_sound():
# import your script A
a = (r"C:\Users\A\Desktop\sound.mp3")
playsound(a)
def CV2_stuff():
# import your script B
os.environ['SDL_VIDEO_CENTERED'] = '1'
cap = cv2.VideoCapture("video.mp4")
...
Thread(target = play_sound).start()
Thread(target = CV2_stuff).start()
希望有帮助
这也可以工作
import ScriptA
import ScriptB
ScriptA.function()
ScriptB.function()
但它们不会在同一时间被灭绝
I have tried using
#!/bin/bash
python ScriptA.py &
python ScriptB.py &
to run both scripts at the same time but it always returns "Invalid Syntax" with ScriptA even though all python files are in the same folder.
File that runs both scripts:
def song():
user = input()
if user == "Chance":
python ScriptA.py &
python ScriptB.py &
else:
print("Error")
The solutions i found so far, such as putting that script in one line, doesn't work as the error still shows.
--------------------------EDIT--------------------------
Both scripts run fine individually however, all the solutions you have provided still run sequentially. Script A is a video that plays via OpenCV and Script B is a song that plays via playsound.
ScriptA:
import cv2
import numpy as np
import os
os.environ['SDL_VIDEO_CENTERED'] = '1'
cap = cv2.VideoCapture("video.mp4")
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
cv2.imshow('Frame',frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
ScriptB:
from playsound import playsound
a = (r"C:\Users\A\Desktop\sound.mp3")
playsound(a)
As you may tell, i'm trying to display a song alongside a video. I tried to display a video that has sound but openCV doesn't output sound for some reason. Any suggestions?
I think you are looking for multi-threading
you could merge your both script into another script, then lauch them using theads
--edit--
from threading import Thread
import cv2
import numpy as np
import os
from playsound import playsound
def play_sound():
# import your script A
a = (r"C:\Users\A\Desktop\sound.mp3")
playsound(a)
def CV2_stuff():
# import your script B
os.environ['SDL_VIDEO_CENTERED'] = '1'
cap = cv2.VideoCapture("video.mp4")
...
Thread(target = play_sound).start()
Thread(target = CV2_stuff).start()
hope it helps
this could work too
import ScriptA
import ScriptB
ScriptA.function()
ScriptB.function()
but they wouldn't be exectuted in the same time
这篇关于同时运行两个python文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!