-1

Imagine the application I am building as a normal media playlist (Video / Music). On the client side, I select the files I want to play (Files are located on server), and I send its paths to the server as an array of objects. Converted to JSON the object would look like:

{
   "mediaFiles":[
      {
         "type":"photo",
         "path":"/../photo.jpg"
      },
      {
         "type":"video",
         "path":"/../video.mp4"
      }
   ]
}

On the server side I will be launching video player or image viewer based on file type. So far everything is simple and clear.

The part where it get's bit tricky is that I will have remote control in my client application. Therefore I need to keep a track of which file is currently playing, and also the array index of that file to be able to integrate all remote play features (play / stop / prev / next).

My current logic was to have my server multi threaded. One thread would be in charge of playing the playlist and keeping the track of it, and another thread would be in charge of remote play.

I normally use Spring and NodeJS for building RESTful API's. I consider using Spring as an over kill only for this feature, and the problem with NodeJS is that it is JavaScript and it doesn't support multi-threading.

Therefore, I decided to switch to Python (Flask) for this problem. Keep in mind that this will be my first touch of Python

Possible problems with this plan:

Like I said, I will have one thread which will be listening for mediaFiles array (sent from client).

  1. Once the array is received, start the while loop and inside it play the files.
  2. In order to stop it, I call stop endpoint, and while loop will break.

  3. How would I be able to use prev, next, and pause features while while loop is running? This is the part why I think this approach isn't good.

I would really like to hear some ideas on how to improve this approach.

  • Really curious why this question got 2 down votes, but no close votes... – Greg Burghardt Jan 10 at 15:04
  • Well, Nodejs doesn't support multithreading, but does support spawning child processes - stackoverflow.com/questions/40028377/… – Pawel Gorczynski Jan 14 at 15:26
  • Why do you need multiple threads? Can't you just send events & heartbeat from the client side? Or just use websockets? – Tjorriemorrie Jan 24 at 22:25
1

You will need to use multi-processing feature in Python.

Your server side application has two parts.

  1. Admin/Management APIs (listener)
  2. Media handling/playing process (which internally supports stop and play operations)

So as you correctly mentioned, you will need to use a multi-threaded model to cater these two requirements. In a single thread model, you will not be able to let the second part to execute continuously.

Hope you will go in the multi-processing direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.