Concurrent programming in Python using Python threads has never been so sweet and easy as this. Today I’ll go over playing a set of movies simultaneously using the threading module provided by python. I have written a program to stream four different movies at the same time using vlc. Below is the code for going about it.

from threading import Thread
import os

movies = os.listdir(‘/home/volsbit/movies’) # returns a list of movies

class mythread(Thread):
        def __init__(self, movie):
                Thread.__init__(self)
                self.movie = movie

        def run(self):
                system(“vlc %s” % self.movie)

# now we create our threads

threads = []

for movie in movies:
        thread = mythread(movie)
        thread.start()
        threads.append(thread)

for th in threads:
        th.join() # a call to join ensures all threads wait for the other threads to finish executing

movies streaming concurrently

Advertisement