×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    thread questions

    hi guys,

    sorry for all the questions recently.
    I want my main program to call a thread which will check gps data from a bluetooth gps device every 30 seconds. I can do this, however I can't kill the new thread, so it keeps running even after the gui has exited. Is there anyway I can signal the new thread to end, or is there a way to manually kill it?

    Thanks in advance

    docGroup14

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    386

    Re: thread questions

    Here's one way that comes to my mind:

    Code:
    import e32
    import thread
    
    def subthread():
        global timer, subthread_active
        timer = e32.Ao_timer()
        while subthread_active:
            # a theoretical function to check the gps data
            check_gps_data()
            # sleep for 30 sec
            timer.after(30)
    
    # start the subthread
    subthread_active = True
    thread.start_new_thread(subthread, ())
    
    # stop the subthread
    subthread_active = False
    timer.cancel()

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Re: thread questions

    cheers, that seems like a decent way to do it

    while you are here, could you tell me how I would go about passing a variable to a thread. For example if I wanted to pass "lock" to a thread when i put:

    thread.start_new_thread(threadName, (lock))

    it says that the second argument must be a tuple. But a tuple of what?!?!

    Thanks

    docGroup14

  4. #4
    Regular Contributor
    Join Date
    Sep 2005
    Location
    Finland, Helsinki
    Posts
    323

    Re: thread questions

    Quote Originally Posted by docGroup14 View Post
    - - could you tell me how I would go about passing a variable to a thread. For example if I wanted to pass "lock" to a thread when i put:

    thread.start_new_thread(threadName, (lock))

    it says that the second argument must be a tuple.
    Simple example:

    Code:
    >>> lock = 1
    >>> type(lock)
    <type 'int'>
    >>> type((lock))
    <type 'int'>
    >>> type((lock,))
    <type 'tuple'>
    Note the ','. Got it?

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Re: thread questions

    thanks - much appreciated

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Re: thread questions

    Quote Originally Posted by y.a.k View Post
    Here's one way that comes to my mind:

    Code:
    import e32
    import thread
    
    def subthread():
        global timer, subthread_active
        timer = e32.Ao_timer()
        while subthread_active:
            # a theoretical function to check the gps data
            check_gps_data()
            # sleep for 30 sec
            timer.after(30)
    
    # start the subthread
    subthread_active = True
    thread.start_new_thread(subthread, ())
    
    # stop the subthread
    subthread_active = False
    timer.cancel()
    hi, thanks for the help so far. but when i use timer.cancel the whole app closes even though there is a lock waiting for the exit key to be pressed.

    any ideas on what might be happening?

    thanks

    docGroup14

    edit: if i take the timer.cancel() out then it works fine but just waits up to 30 seconds before stopping the gps thread
    Last edited by docGroup14; 2007-10-18 at 14:10.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    386

    Re: thread questions

    Ok, I've checked my code on my phone now and you're right - it crashes.

    It seems that e32.Ao_timer cannot be used across threads. This would be logical since AFAIK the Active Objects it is based on are thread specific (i.e. every thread has its own Active Objects pool).

    But do you really need to use threads? Let's just stay with Active Objects. Here's an example (checked):
    Code:
    import e32
    import appuifw
    
    def timerfunc():
        print 'check gps here'
        timer.after(30, timerfunc)
    
    timer = e32.Ao_timer()
    timerfunc()
    
    appuifw.app.menu = [(u'Stop', timer.cancel)]
    
    lock = e32.Ao_lock()
    appuifw.app.exit_key_handler = lock.signal
    lock.wait()
    The trick here is to use the timer. Since we're staying in lock.wait() most of the time, the timer will get called as expected (because both of them are based on Active Objects, hence the "Ao_" in their names). Every time the timer gets called, it does its job and resets itself to get called in the next 30 sec.

    Any args for the timer could be passed as global variables. If you don't like it, here's another way:
    Code:
    def timerfunc(arg1, arg2):
        print 'check gps here'
        print 'args:', arg1, arg2
        timer.after(30, lambda: timerfunc(arg1, arg2))
    Note that because now we pass two args to timer.after(), it returns immediately (read PyS60 docs for more info).
    Last edited by y.a.k; 2007-10-18 at 15:03.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Re: thread questions

    thanks for the info, the only reason i wanted to use threads was because i want the gps program to run in the background, and i wish for users to be able to use other functions of my program while it is running. but if i understand you correctly, this will still be possible due to the fact that we are using the timer to wait. i.e. while we are waiting the cpu can be used for other functions.

    if that is indeed the case then i will use your first alternative method as i have no arguments to pass.

    thanks again for your help

    docGroup14

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    386

    Re: thread questions

    As far as I understand active objects, this code will block your program for the time you're doing the gps thingies. So the question is whether it takes long enough for the user to notice or not.

    If it takes too long, you could start a new thread from the timer and do the gps stuff there. This way you would wait in the main thread using the timer (with the possibility to easily stop it) but do the stuff in a separate thread.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Re: thread questions

    thanks, i had noticed that during testing. I will see how it goes and may use another thread.

    thanks for the help

    docGroup14

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Re: thread questions

    1 final question if i may:
    if i were to use another thread is there anyway to get the pid of the new thread so i can use thread synchronisation (i.e. wait on the pid of the new thread)?

    the reason i ask is that this would probably work better in a new thread but i can't find any way of signaling when the thread ends. I could use a while loop to check a variable but this seems quite hacky.

    ultimately i want to launch a new thread, then when it ends schedule a new one to be made after 30 seconds. at the moment i have no way of knowing if the thread has ended or not.

    as you can't share locks between threads is there any way to do the above?

    if possible i need a general solution: i.e. you can use it for a thread made of a method in your main program or in an imported module.

    thanks

    docGroup14

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    386

    Re: thread questions

    Here's one way to achive it (tested):
    Code:
    import thread
    import time
    
    def subthread():
        time.sleep(3)
        print 'subthread ends'
        lock.release()
    
    lock = thread.allocate_lock()
    
    # start the subthread
    lock.acquire()
    thread.start_new_thread(subthread, ())
    
    # check if it's running
    print lock.locked()
    
    # wait for the subthread to finish
    lock.acquire()
    If it should be used for a thread made of some callable, I would made these changes:
    Code:
    def subthread(target):
        target()
        lock.release()
    
    thread.start_new_thread(subthread, (some_module.some_function,))

Similar Threads

  1. Emulator crash immediatly after launched, SDK of S60 3nd
    By loopfinder in forum Symbian Tools & SDKs
    Replies: 27
    Last Post: 2011-01-30, 14:06
  2. S60 SDK 3rd edition FP1 Emulator problem
    By justteam in forum Symbian Tools & SDKs
    Replies: 14
    Last Post: 2010-03-23, 08:47
  3. console app running problem
    By xcompass in forum Symbian
    Replies: 3
    Last Post: 2009-05-08, 13:26
  4. Error on "emulator"
    By zhang2008 in forum Symbian
    Replies: 2
    Last Post: 2006-10-08, 16:55
  5. Link errors when trying to Display bitmap
    By Bkc82 in forum Symbian Media (Closed)
    Replies: 1
    Last Post: 2006-01-16, 22:46

Posting Permissions

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