×

Discussion Board

Results 1 to 11 of 11

Thread: Thread example

Hybrid View

  1. #1
    Regular Contributor
    Join Date
    Oct 2004
    Posts
    158

    Thread example

    In my app I'm trying to write to a file in a seperate thread, but somehow I can't get threading to work correctly. Basically what I'm doing is:

    ___________________________________
    import thread

    thread.start_new_thread(self.write, ())

    def write(self):
    for i in range(2):
    appuifw.app.body.set(u"Running: ", i)
    time.sleep(5)
    ___________________________________

    When I use this method the UI freezes. I read the piece about active objects in the manual, but that doesn't seem to be what I want. Any suggestions or simple example how to get threads working?

    Cheers,
    Berco

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    Python for Series 60 only allows you to access an open file from the thread in which you opened it. Could you perhaps open the file in the worker thread?

    For logging to a file from several threads, I have found the simplest approach to be to open the file in append mode before each write and close it afterwards. That can be bulletproofed by wrappinng the whole thing in a mutex.

    Cheers,

    Ken Rimey

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    But sorry, I didn't read your code snippet when I wrote the reply above. The code seems to have a number of problems, and I'm not sure how it relates to your question. You'll need to clarify.

    -Ken

  4. #4
    Regular Contributor
    Join Date
    Oct 2004
    Posts
    158
    What I want to accomplish is a thread running in the background that writes something to a text file every few seconds. How do you do that without freezing the UI? The skeleton example that comes with the installation is not a useful example for that.

    I get the feeling that there's something wrong with the way I create new threads since they freeze the UI.

    Cheers,
    Berco

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    Having a thread write something to a text file occasionally should not be a problem. The problem may lie elsewhere. Could you please post the code in question? The snippet you included in your original message does not write to a file and doesn't actually run at all. -Ken

  6. #6
    Regular Contributor
    Join Date
    Oct 2004
    Posts
    158
    Hi Ken,

    Sorry, here's the code, which seems to be working:

    Code:
    import e32
    import time
    import thread
    import appuifw
    
    e32.ao_yield() 
    
    class Test:
        
        def __init__(self):
            self.lock = e32.Ao_lock()
            appuifw.app.exit_key_handler = self.abort
            appuifw.app.body = appuifw.Text()
            appuifw.app.body.set(u"App started")
            appuifw.app.menu = [(u"Exit", self.abort)] 
            thread.start_new_thread(self.writeToFile, ())
            # enter loop to prevent finishing the app
            self.loop()
    
        def loop(self):
            self.keepRunning = True
            while self.keepRunning:
                self.refresh()
                self.lock.wait()
        
        def refresh(self):
            # update UI if necessary    
            appuifw.app.body.set(u"App running: " + time.strftime("%H:%M:%S\n"))
                
        def writeToFile(self):
            self.keepRunning = True
            while self.keepRunning:
                self.output = open("c:\\tmp\\output.txt", "a")
                self.output.write(time.strftime("%H:%M:%S\n"))
                self.output.close()
                time.sleep(5)
    
        def abort(self):
            self.keepRunning = False
            appuifw.app.exit_key_handler = None
            self.lock.signal()
            
    ################
    def main():
        Test()
        
    if __name__ == "__main__":
        main()

    I'm not sure how elegant this is....
    One thing I forgot was putting the main application in a loop. I'm also wondering whether UI changes will be processed and visible in this way.

    Cheers,
    Berco

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    bercobeute wrote:
    ...I'm also wondering whether UI changes will be processed and visible in this way.
    Sure, UI changes will be processed and made visible as long as you are blocked on wait(). The tricky part is making the changes to the UI state, which is only safe to access in the main thread. Feasible places to do it in the particular style you are using are event handlers and your refresh method. In the latter case, you have to signal the lock (which you can do from any thread) whenever you want the refresh method to execute. -Ken

  8. #8
    Regular Contributor
    Join Date
    Oct 2004
    Posts
    158
    Great info. Thanks, Ken.

  9. #9
    Regular Contributor
    Join Date
    Jan 2005
    Location
    Bangkok, Thailand
    Posts
    148
    Is it necessary to use e32.ao_sleep(5) instead of time.sleep(5) ?

  10. #10
    Regular Contributor
    Join Date
    Oct 2004
    Posts
    158
    No, I don't think so. It's just that I tried e32.ao_sleep() instead of time.sleep() because I couldn't get it to work. But that didn't solve the problem. Now that I solved it I found out that time.sleep() works just as well.

    I'm not exactly sure what the difference between both is. Anybody?

    Cheers,
    Berco

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    1

    Re: Thread example

    I got this error while Installing the jar in pda "Fatal error: KErrCorrupt"
    i need the Solution immediately.............
    please send now itself.. thanks..

Posting Permissions

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