简体   繁体   中英

How to run three ".py" files with sound concurrently?

First, my English might be awkward. Sorry.
I have a pi zero w.
When the Raspberry Pi is powered on, there are 3 .py files to automatically run.
but have some problems.

A.py , B.py , C.py

All three have the ability to play sound with mpg123 .
A and B are waiting for each button input, and C continues to work as a sensor.
In C , the sound continues, and if there is a button press in A OR B , kill C and bring it back life.

Problems:

  1. If I use python ~.py & and try to run another .py, the sound of the .py that goes back to the background doesn't work, and the function of that file doesn't work. But if I check with ps -ef , the file is alive.

    Exactly, it stops at the mpg123 command. mpg123 Basic information comes out, then it doesn't play, and then .py stops.

  2. I put the code that kills C.py in A.py or B.py saves it again after the work is finished, but it breaks at the second operation.

When the A and B buttons are pressed, I just want C's mpg123 to be quiet. And I want C to work when it ends at A and B.

Code A.py - email_send


import subprocess
from subprocess import call


def sendmail():
  now_ip = call(["hostname", "-I"])
  myip = now_ip + "~"
  call(["wget", "-O", "test_img.jpeg", myip])
  

try:
 while True:
  if GPIO~ A button Push:
    call(["pkill","-9","-ef","C.py"])
    call(["pkill","-9","-ef","C.mp3"])
    subprocess.Popen(["mpg123", "A.mp3"])
    sendMail()
    call(["python","C.py"])
except

Code B.py - data send

# Similar to A

Code C.py - i2c

import subprocess import call

while True:
 if 0<~<100:
   call(["mpg123", "C.mp3"])

There is no problem if you run them individually, but if you use nohup or & to run them together, it will be broken.

I want to stop the sound by giving a signal to C.py when the buttons of A.py and B.py are pressed or when the task is finished. I understand in my head, but I don't know why.

How can I do this?

Thanks to him.
It's not perfect becuase my first multithreading, but the solution I was looking for was correct.

import RPi.GPIO as GPIO
import sys
import signal
import time
from email_test import sendMail
from razer_test import runtof
import subprocess
from subprocess import call
import threading

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
push_red = 22
push_white = 23
GPIO.setup(push_red, GPIO.IN)
GPIO.setup(push_white, GPIO.IN)

email_flag = 0 # control flag for Problem 1.
main_flag = 0 

def Mail(self):
    global email_flag
    while True:
        if GPIO.input(push_red) == 0:
                email_flag = 1
                subprocess.Popen(["mpg123", "Email_voice.mp3"])
                sendMail() #code A.py
                email_flag = 0
        time.sleep(0.1)

def razer(self):
    global email_flag
    global main_flagg
    runtof(email_flag,main_flag) #code C.py

thread_Mail = threading.Thread(target=Mail)
thread_Mail.start()
thread_razer = threading.Thread(target=razer)
thread_razer.start()
'''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM