繁体   English   中英

如何同时运行三个带有声音的“.py”文件?

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

首先,我的英语可能很笨拙。 对不起。
我有一个 pi 零 w。
树莓派开机后,有3个.py文件自动运行。
但有一些问题。

A.py , B.py , C.py

这三个都可以使用mpg123播放声音。
AB正在等待每个按钮输入,而C继续作为传感器工作。
C ,声音继续,如果在AB按下按钮,则杀死C并使其恢复生机。

问题:

  1. 如果我使用python ~.py &并尝试运行另一个.py,则返回后台的.py 的声音不起作用,并且该文件的功能不起作用。 但是,如果我使用ps -ef检查,则该文件还活着。

    确切地说,它在mpg123命令处停止。 mpg123基本信息出来了,然后不播放了,然后.py就停止了。

  2. 我把杀掉C.py的代码放在A.py或者B.py ,工作完成后再次保存,但是第二次操作就断了。

当按下 A 和 B 按钮时,我只希望 C 的 mpg123 安静。 我希望 C 在 A 和 B 结束时工作。

代码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

代码B.py - 数据发送

# Similar to A

代码C.py - i2c

import subprocess import call

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

单独运行是没有问题的,但是如果用nohup或者&一起运行,就会坏掉。

当按下 A.py 和 B.py 的按钮或任务完成时,我想通过向 C.py 发出信号来停止声音。 我心里明白,但我不知道为什么。

我怎样才能做到这一点?

感谢他。
这并不完美,因为我的第一个多线程,但我正在寻找的解决方案是正确的。

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()
'''

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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