fa001
Posts: 79
Joined: Wed Jan 02, 2013 8:25 pm

Two threads one keyboard ?

Fri Feb 01, 2013 5:38 pm

I want to allow for selection of next song while one song is playing. So I was thinking of putting the music player in one thread and the selection routine in another thread. However both can use keyboard input. So how does one direct keyboard input to a specific selected thread?

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Two threads one keyboard ?

Mon Feb 04, 2013 5:42 pm

Third thread for handling the keyboard and sending commands to the other two depending on the input?
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

fa001
Posts: 79
Joined: Wed Jan 02, 2013 8:25 pm

Re: Two threads one keyboard ?

Tue Feb 05, 2013 5:22 pm

Thanks for the reply.
The third thread is a good idea however my question is how does one direct input to a specific thread?
I am a bit lost in pipes and FIFOs so I was looking for something easy :)

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Two threads one keyboard ?

Tue Feb 05, 2013 5:57 pm

So you are planning to incorporate an external 'ready-made' player as part of your system? And that player has some keyboard control? And you want to implement a song selection mechanism, which would grab keyboard input and not let it go to the player? Hmm... that might be a bit more complicated - depending on the player...
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4209
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Two threads one keyboard ?

Tue Feb 05, 2013 6:07 pm

Probably your best plan would be for one or other of the threads to be responsible for the keyboard, and if they get a key they are not interested in they pass it to the other thread. Whether you use pipes for that is up to you, but it is probably the easiest way; multi-threading contention is a complex subject.

A simple solution would be to wrap the keyboard routines in a mutex, so only one thread could be in there at a time. The player thread might be in and out quickly, whereas the selection thread could keep it locked while the selection was happening. During which time the player thread repeatedly asks for the mutex, but times out and goes about its other business.

Return to “Python”

Who is online

Users browsing this forum: Baidu [Spider], raissahbibi and 20 guests