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?
You can create a fourth python file d.py in the same folder as other 3 python files, which imports the other 3 python files and runs their functions, as shown below. In this article, we have learnt how to run multiple python files.
The simplest solution to run two Python processes concurrently is to run them from a bash file, and tell each process to go into the background with the & shell operator.
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
You have to import os
module and use the system
function from it, then separate the two Python files you are running by &&
.
import os
def song():
user = input()
if user == "Chance":
os.system('python ScriptA.py && python ScriptB.py')
else:
print("Error")
song()
But I will advice you to just import the two files you want to run into the third file and just run the functions in there like normal functions.
e.g.
import ScriptA
import ScriptB
ScriptA.function()
ScriptB.function()
If there are no functions inside the scripts, the script runs immediately after they are imported.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With