在Python中同时启动两个方法 [英] Starting two methods at the same time in Python

查看:217
本文介绍了在Python中同时启动两个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Python中同时运行两个方法.其中一个播放声音,另一个录制声音.两种方法都可以正常工作,但是我无法弄清楚如何在尝试多处理和线程化的同时启动它们.而我现在几乎可以肯定,线程无法解决该问题.

I am trying to run two methods at the same time in Python. One of them plays a sound and the other one records it. Both methods work fine but I could not figure it out how to start them at the same time both multiprocessing and threading were tried. Whereas I am almost sure now that it can't be solved with threading.

def listen_to_audio()

def play_audio()

有什么想法吗? (它们不必同时完成,但是它们都应该在一秒钟内开始.)

Any ideas? (They don't have to finish at the same time but they should both start within a second.)

那是代码,很抱歉没有在一开始就发布它:

That is the code, sorry for not posting it in the beginning:

import pyaudio
import wave
import sys
import time
from math import *
from getopt import *
import threading

def make_sin(f0=1000.,ampl=30000,rate=22050,length=5.):
  a = 2. * pi * f0/rate              
  n = int(rate * length)
  wav=''
  for i in range(0, n):
    f = int(ampl*sin(a*i))
    wav += chr(f & 0xFF) + chr((f & 0xFF00) >> 8)
  return wav



def play_audio(forHowLong):
    data = make_sin(f0=1000.,ampl=30000,rate=22050,length=5.)

    p = pyaudio.PyAudio() #sets up portaudio system
    stream = p.open(format=p.get_format_from_width(2),
                channels=1,
                rate=22050,
                output=True)

    start = time.time()
    while time.time() < start + forHowLong*0.5:    
            stream.write(data)

    stream.stop_stream()
    stream.close()
    p.terminate()


def listen_to_audio(forHowLong):
    CHUNK = 1024
    FORMAT = pyaudio.paInt16
    CHANNELS = 2
    RATE = 44100
    RECORD_SECONDS = forHowLong
    WAVE_OUTPUT_FILENAME = "output.wav"

    p = pyaudio.PyAudio()

    stream = p.open(format=FORMAT,
            channels=CHANNELS,
            rate=RATE,
            input=True,
            frames_per_buffer=CHUNK)

    print("* recording")

    frames = []

    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data = stream.read(CHUNK)
        frames.append(data)

    print("* done recording")

    stream.stop_stream()
    stream.close()
    p.terminate()

    wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(b''.join(frames))
    wf.close()


def main():

#start play_audio and record_audio here at the same time



if __name__ == "__main__":
    main()

推荐答案

import threading,time
def play1():
    while time.time() <= start_time:
        pass
    threading.Thread(target=listen_to_audio).start()
def play2():
    while time.time() <= start_time:
        pass
    threading.Thread(target=play_audio).start()
start_time=time.time()+20
threading.Thread(target=play1).start()
threading.Thread(target=play2).start()

这应该为您工作,它启动每个功能,并在每个功能中等待,直到合适的时间:)

This should work for you, it starts each function, and in each function it waits until it is the right time :)

这篇关于在Python中同时启动两个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆