6

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?

A.J
  • 241
  • 2
  • 3
  • 10
  • 1
    Both scrips are working fine when you execute them individually? – Mufeed Apr 17 '18 at 11:08
  • What language is that second code snippet? Or, rather, what language is it intended to be? It is invalid syntax in both sh and python. – William Pursell Apr 17 '18 at 11:18
  • @Mufeed asks the right questions. "Invalid Syntax" error has nothing to do with files being in the same folder. Probably you have a python syntax error in your ScriptA.py , as your provided python script also has syntax error in it. – atakanyenel Apr 17 '18 at 11:34
  • To clarify, both scripts work perfectly fine when executed individually. – A.J Apr 17 '18 at 21:14
  • Due to GIL(Global Interpreter Lock) in python only one thread can run at a given time, check this out [threads not able to reduce the run time of two function when run at once](https://stackoverflow.com/questions/58005044/threads-not-able-to-reduce-the-run-time-of-two-function-when-run-at-once) – Santhosh Dhaipule Chandrakanth Sep 19 '19 at 10:14

4 Answers4

9

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

Peko Chan
  • 306
  • 5
  • 15
  • I tried that but still no luck, i have edited my original post to help clarify my situation – A.J Apr 18 '18 at 12:53
  • My soltion here would be to make one script with 2 functions, then call them with theards, i'll edit my post too :) – Peko Chan Apr 18 '18 at 13:12
  • 1
    Thank you so much Peko, it works! Now all i have to do is figure out how to loop the video until the song ends so the video can end and return to another script – A.J Apr 18 '18 at 13:23
3

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.

  • this will not run them parallelly as intended by OP. You should probably call the functions in two different threads. – penguin2048 Apr 17 '18 at 11:28
  • As penguin stated, it does not run parallel, i have included more information in my original post. – A.J Apr 18 '18 at 12:55
1

You can try os.system :

import os

os.system("python ScriptA.py &")
os.system("python ScriptB.py &")
NSM
  • 31
  • 5
  • I tried that and it sill shows sequentially. I have updated my original post to help understand what i am trying to do – A.J Apr 18 '18 at 12:54
0

You can just open both files on the python IDLE and run each o them. If you need both files to run simultaneously (the first way you have the delay of pressing F5 on each file) you can use PyCharm and download the multirun plugin.