turning my multithreading code into multiprocessing /u/qhelspil Python Education

i want to record 2 audios at the same time.

using multithreadind this code works fine

import sounddevice as sd from scipy.io.wavfile import write import numpy as np import threading fs = 44100 duration = 5 filename1 = ‘output.wav’ filename2 = ‘second_output.wav’ def record_audio(fs, duration, filename): print(f”Recording first audio to {filename}…”) recording = sd.rec(int(duration * fs), samplerate=fs, channels=2, dtype=’float64′) sd.wait() # Wait until the recording is finished print(“First recording stopped.”) recording = np.int16(recording * 32767) write(filename, fs, recording) print(f”First audio saved as {filename}”) thread1 = threading.Thread(target=record_audio, args=(fs, duration, filename1)) thread2 = threading.Thread(target=record_audio, args=(fs, duration, filename2)) thread1.start() thread2.start() thread1.join() thread2.join() print(“recordings complete.”)

using multiprocessing, i just replaced the classes and the result is

import sounddevice as sd from scipy.io.wavfile import write import numpy as np from multiprocessing import Process fs = 44100 duration = 5 filename1 = ‘output.wav’ filename2 = ‘second_output.wav’ def record_audio(fs, duration, filename): print(f”Recording audio to {filename}…”) recording = sd.rec(int(duration * fs), samplerate=fs, channels=2, dtype=’float64′) sd.wait() print(f”Recording stopped for {filename}.”) recording = np.int16(recording * 32767) write(filename, fs, recording) print(f”Audio saved as {filename}”) process1 = Process(target=record_audio, args=(fs, duration, filename1)) process2 = Process(target=record_audio, args=(fs, duration, filename2)) process1.start() process2.start() process1.join() process2.join() print(“recordings complete.”)

however it is not recording anything, it prints ‘recordings completed’ instantly

what am i doing wrong?

thanks

submitted by /u/qhelspil
[link] [comments]

​r/learnpython i want to record 2 audios at the same time. using multithreadind this code works fine import sounddevice as sd from scipy.io.wavfile import write import numpy as np import threading fs = 44100 duration = 5 filename1 = ‘output.wav’ filename2 = ‘second_output.wav’ def record_audio(fs, duration, filename): print(f”Recording first audio to {filename}…”) recording = sd.rec(int(duration * fs), samplerate=fs, channels=2, dtype=’float64′) sd.wait() # Wait until the recording is finished print(“First recording stopped.”) recording = np.int16(recording * 32767) write(filename, fs, recording) print(f”First audio saved as {filename}”) thread1 = threading.Thread(target=record_audio, args=(fs, duration, filename1)) thread2 = threading.Thread(target=record_audio, args=(fs, duration, filename2)) thread1.start() thread2.start() thread1.join() thread2.join() print(“recordings complete.”) using multiprocessing, i just replaced the classes and the result is import sounddevice as sd from scipy.io.wavfile import write import numpy as np from multiprocessing import Process fs = 44100 duration = 5 filename1 = ‘output.wav’ filename2 = ‘second_output.wav’ def record_audio(fs, duration, filename): print(f”Recording audio to {filename}…”) recording = sd.rec(int(duration * fs), samplerate=fs, channels=2, dtype=’float64′) sd.wait() print(f”Recording stopped for {filename}.”) recording = np.int16(recording * 32767) write(filename, fs, recording) print(f”Audio saved as {filename}”) process1 = Process(target=record_audio, args=(fs, duration, filename1)) process2 = Process(target=record_audio, args=(fs, duration, filename2)) process1.start() process2.start() process1.join() process2.join() print(“recordings complete.”) however it is not recording anything, it prints ‘recordings completed’ instantly what am i doing wrong? thanks submitted by /u/qhelspil [link] [comments] 

i want to record 2 audios at the same time.

using multithreadind this code works fine

import sounddevice as sd from scipy.io.wavfile import write import numpy as np import threading fs = 44100 duration = 5 filename1 = ‘output.wav’ filename2 = ‘second_output.wav’ def record_audio(fs, duration, filename): print(f”Recording first audio to {filename}…”) recording = sd.rec(int(duration * fs), samplerate=fs, channels=2, dtype=’float64′) sd.wait() # Wait until the recording is finished print(“First recording stopped.”) recording = np.int16(recording * 32767) write(filename, fs, recording) print(f”First audio saved as {filename}”) thread1 = threading.Thread(target=record_audio, args=(fs, duration, filename1)) thread2 = threading.Thread(target=record_audio, args=(fs, duration, filename2)) thread1.start() thread2.start() thread1.join() thread2.join() print(“recordings complete.”)

using multiprocessing, i just replaced the classes and the result is

import sounddevice as sd from scipy.io.wavfile import write import numpy as np from multiprocessing import Process fs = 44100 duration = 5 filename1 = ‘output.wav’ filename2 = ‘second_output.wav’ def record_audio(fs, duration, filename): print(f”Recording audio to {filename}…”) recording = sd.rec(int(duration * fs), samplerate=fs, channels=2, dtype=’float64′) sd.wait() print(f”Recording stopped for {filename}.”) recording = np.int16(recording * 32767) write(filename, fs, recording) print(f”Audio saved as {filename}”) process1 = Process(target=record_audio, args=(fs, duration, filename1)) process2 = Process(target=record_audio, args=(fs, duration, filename2)) process1.start() process2.start() process1.join() process2.join() print(“recordings complete.”)

however it is not recording anything, it prints ‘recordings completed’ instantly

what am i doing wrong?

thanks

submitted by /u/qhelspil
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *