yali film
yali film

Reputation: 1

How can I run two different code in Python at the same time?

I want to run winsound and print an output at the same time. Here's my code:

winsound.PlaySound(filename, winsound.SND_FILENAME)
print('a')

How would I do this?

Upvotes: 0

Views: 66

Answers (2)

incarnadine
incarnadine

Reputation: 700

You need to use threading as shown in this answer.

import winsound
from threading import Thread

def play_sound():
    winsound.PlaySound(filename, winsound.FILENAME)

thread = Thread(target=play_sound)
thread.start()
print ('a')

Upvotes: 1

Tirterra
Tirterra

Reputation: 696

pip install multiprocessing

import multiprocessing
import time

def random_stuff_to_do():
    time.sleep(1)
    print("Slept 1 second")

task1 = multiprocessing.Process(target = random_stuff_to_do)
task2 = multiprocessing.Process(target = random_stuff_to_do)

task1.start()
task2.start()

task1.join() #This part is usefull only if you want to wait for your tasks to end 
task2.join() #before the program to continue.

print("Task 1 and 2 done at the same time")

Upvotes: 0

Related Questions