Python Forum
How to execute several fonctions at the same time - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How to execute several fonctions at the same time (/thread-31525.html)



How to execute several fonctions at the same time - yoyo - Dec-16-2020

hello,
I have a python project where I should use this fonction to make musics.
from simpleaudio import *
import numpy as np

def sound(freq, duration):
    sample_rate = 44100
    t = np.linspace(0, duration, int(duration * sample_rate), False)
    tone = np.sin(freq * t * 2 * np.pi)
    tone *= 8388607 / np.max(np.abs(tone))
    tone = tone.astype(np.int32)
    i = 0
    byte_array = []
    for b in tone.tobytes():
        if i % 4 != 3:
            byte_array.append(b)
        i += 1
    audio = bytearray(byte_array)
    play_obj = play_buffer(audio, 1, 3, sample_rate)
    play_obj.wait_done()
I want to integrate this function in a Tkinter window to play music while displaying notes on the screen. I know how to display the note but my function sound block my Tkinter window.
So how should I call my function sound to execute the two at the same time ?


RE: How to execute several fonctions at the same time - Larz60+ - Dec-16-2020

I think you want to use multiprocessing (not threading) here.
see: https://docs.python.org/3/library/multiprocessing.html


RE: How to execute several fonctions at the same time - buran - Dec-16-2020

https://python-forum.io/Thread-Tkinter-How-to-deal-with-code-that-blocks-the-mainloop-freezing-the-gui


RE: How to execute several fonctions at the same time - yoyo - Dec-22-2020

Thanks for your answers, I read those two links, but I'm a beginner in python so I still don't understand how those two methods works and how I can use it for my program. Can someone explain it to me more simply?