Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: threading in python

  1. #1
    Join Date
    Nov 2011
    Beans
    4

    threading in python

    Hi, I want to create a python wrapper for mplayer, which will read subtitles for movies through speech dispatcher. It is mainly for visually impaired people like me. I am using mplayer.py module for controlling mplayer in slave mode and pysub module for accessing .srt files. I want to create simple ncurses interface, which will just wait for certain keyboard shortcuts for quitting, pausing, stopping, changing of voice rate etc. But now how to do this? I have two loops: curses waiting for keypresses and another waiting for right time, mplayer for the time of movie. I have put the second loop into thead and it looks like this: class threadsub(threading.Thread): def __init__(self): super(threadsub, self).__init__() self._stop = threading.Event() def stop(self): self._stop.set() def stopped(self): return self._stop.isSet() def run(self): global playing for i in range (0, len(subs) - 1): ms = float(subs[i].start.milliseconds) ms = round(ms / 1000,1) time = subs[i].start.hours * 3600 + subs[i].start.minutes * 60 + subs[i].start.seconds + ms while playing == True: if time == round(p.time_pos,1): s.speak(subs[i].text) break if self.stopped() == True: playing = False if self.stopped() == True: break When i exit the program, i end curses and call threadsub.stop() But it doesn't stop, nor it raises an error. Can you please gi me an advice, how to do this properly? Or can you suggest other method for achieving this? Forgive my possible mistakes when pasting code, I am posting something like this for the first time. Thank you.
    Last edited by krecoun; March 27th, 2012 at 05:03 PM. Reason: making code more readable

  2. #2
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: threading in python

    @OP: Please start by making your code more readable. Put your code between "CODE" tags and make sure that the indentation is kept. If not, edit your post afterwards. After this process, we can actually see what is happening in your code.

  3. #3
    Join Date
    Mar 2012
    Location
    Russia
    Beans
    104
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: threading in python


  4. #4
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: threading in python

    Quote Originally Posted by CynicRus View Post
    You might want to elaborate on how this link is supposed to help the OP, as that tools cannot do what the OP wants to achieve with his/her project.

  5. #5
    Join Date
    Mar 2012
    Location
    Russia
    Beans
    104
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: threading in python

    Quote Originally Posted by Zugzwang View Post
    You might want to elaborate on how this link is supposed to help the OP, as that tools cannot do what the OP wants to achieve with his/her project.
    I want to create simple ncurses interface, which will just wait for certain keyboard shortcuts for quitting, pausing, stopping, changing of voice rate etc. But now how to do this?
    The project is the link contains all the necessary information to work with mplaнer with python. But I could misunderstand the question, English is not my native language, (
    Last edited by CynicRus; March 28th, 2012 at 11:20 AM.

  6. #6
    Join Date
    Nov 2011
    Beans
    4

    Re: threading in python

    Well, I am satisfied with my mplayer solution with provided module.
    But I can't convince my program to exit in normal way, I think that the thread is still running, however, I am not experienced enough with debugging to prove it.
    I am attaching the code.
    Thank you.

  7. #7
    Join Date
    Nov 2011
    Beans
    4

    Re: threading in python

    Here it is sorry, I am confused of this forum.

  8. #8
    Join Date
    Nov 2011
    Beans
    4

    Re: threading in python

    attachment
    Attached Files Attached Files

  9. #9
    Join Date
    Mar 2012
    Location
    Russia
    Beans
    104
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: threading in python

    If you need force kill thread - read http://docs.python.org/library/threa....Thread.daemon

  10. #10
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Post Re: threading in python

    Quote Originally Posted by CynicRus View Post
    If you need force kill thread - read http://docs.python.org/library/threa....Thread.daemon
    There is an easier, and probably better way: make "running" a class attribute of "threadsub". You would then have a code that looks like this:

    PHP Code:
    ...
    class 
    threadsub(threading.Thread):
        
    running True    
        def run
    (self):
            
    playing True
            
    for i in range (0len(subs) - 1):
                ... 
    Then, when you call "endcurses()", you could set "t.running = False". The next time your thread executes the line "if running == False", it will take the "if" and end the thread. You might need to replace that line by "if self.running == False", though.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •