×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    2

    Problem with GPS and threads

    Hi, I'm writing application witch will using external GPS. I need one thread which will read GPS info about my position and send it somewhere.
    I'm using this simple code for reading GPS data:
    Code:
    import socket, btsocket, e32, appuifw, thread
    
    
    def init_gps_device():
        address, services = btsocket.bt_discover()
        gps_receiver = (address, services.values()[0]) #select external bt gps receiver
        connection = btsocket.socket(btsocket.AF_BT, btsocket.SOCK_STREAM)
        connection.connect(gps_receiver)
        return connection
    
    def get_position():
        while True:
            ch = connection.recv(1)
            gps_data = ""
            while ch!='\n' :
                gps_data+=ch
                ch=connection.recv(1)
            if gps_data.startswith("$GPGGA"): #check if gps_data is valid NMEA sentence
                gps_data = gps_data.split(",")
                print "your position is %s, %s" % (gps_data[2], gps_data[4])
    
    connection = init_gps_device()
    position = get_position()
    It works great, but problem is when i'm going to use get_position function as external thread like this:
    Code:
    thread.start_new_thread(get_position, ())
    In this case my phone nokia E51 hung! I have no idea what can be wrong :/

  2. #2
    Super Contributor
    Join Date
    Nov 2007
    Location
    Sertaozinho/Brazil
    Posts
    768

    Re: Problem with GPS and threads

    Hello mjetek !

    Are you running from Pyhton shell ?
    We have some problems related to shell and multi tasking applications. Please, read this forum thread.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    2

    Re: Problem with GPS and threads

    I still have problem with it. I made sis using ensymble_gui.pyw but still if i want to run another thread in background my phone hung.

  4. #4
    Super Contributor
    Join Date
    Nov 2007
    Location
    Sertaozinho/Brazil
    Posts
    768

    Re: Problem with GPS and threads

    I never used GPS and multi thread. Besides, the only thread module I have used was threading. So I think I can not help
    Let's wait for other users reporting their experiences with GPS and threads.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    166

    Re: Problem with GPS and threads

    i would like to know how to use threads right too
    i already asked that earlier but have given up after i ve tried to run it from shellskript i packed in core mode

    if anybody could write a tutorial which explains how to run several functions at the same time it would help a lot

  6. #6
    Super Contributor
    Join Date
    Nov 2007
    Location
    Sertaozinho/Brazil
    Posts
    768

    Re: Problem with GPS and threads

    Quote Originally Posted by AJ____92 View Post
    if anybody could write a tutorial which explains how to run several functions at the same time it would help a lot
    This link may help you !

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    166

    Re: Problem with GPS and threads

    and what should i do if i want to manipulate some global / local data (in class)
    are statemants like

    obj.x = 5

    allowed in threads ?

    a few questions

    1. how many threads can i run at the same time ?
    2. 2 or more threads run at the same time, without slowing down each other ?
    3. global/local data can be changed from threads ?
    4. do i have access to modules in threads ?
    5. final question what is the best way of using threads ? (i mean should i use queues? and with what should i be careful if i run 3 or more threads and one of them needs 50 - 90% CPU ?)

    thx in advance and sorry for offtopic

  8. #8
    Super Contributor
    Join Date
    Nov 2007
    Location
    Sertaozinho/Brazil
    Posts
    768

    Re: Problem with GPS and threads

    Quote Originally Posted by AJ____92 View Post
    1. how many threads can i run at the same time ?
    2. 2 or more threads run at the same time, without slowing down each other ?
    3. global/local data can be changed from threads ?
    4. do i have access to modules in threads ?
    5. final question what is the best way of using threads ? (i mean should i use queues? and with what should i be careful if i run 3 or more threads and one of them needs 50 - 90% CPU ?)

    thx in advance and sorry for offtopic
    It is not off topic at all. But I do not have all answers

    1) It is an O.S. question. I don't know the limit, unfortunately. But I believe memory or cpu availability will run out first. It is better to check the source code or wait for better answers. This link has an interesting discussion.

    2) No. Single core CPUs will divide its resources among all running threads. If you have two intensive threads running it is possible to see them slower. If they are not intensive it is likely you can not see this effect. Current processors used in Nokia phones are single core but it is important to know that thread module does not use multi core CPU effectively. Search for python and GIL to have some answers and insights about this topic and about python thread scheduler.

    3) global: ok. But take care with concurrent access. Semaphores may help to create critical sections. I am not sure if I understood the "local sharing" issue. Are you talking about static members ?

    4) If you search the forum you will find several modules that does not work as expected when used inside a thread. For instance, modules related to UI do not work. But I think I can't create a list of these modules.

    5) I am afraid to say that it quite difficult to formulate a general answer to this question. Per case approach may be better.

    Other answers are welcome, dibo guys !

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    166

    Re: Problem with GPS and threads

    thx for the fast answers :P

    i meant with local data:

    i have a class and the data is stored in the class in the mainscript
    if i call a thread and the threads function need to access a var in the object of the class it will work ?



    and about the speed lets say i have 10 functions that need 1 second to get through and each of those 10 functions needs 10 % CPU
    so with threads i'll need less than 0.2 second ? if i go through all functions at the same time ?


    EDIT:
    you realy should help me out with threads you seem to know a lot about that :P
    Last edited by AJ____92; 2010-02-01 at 21:25. Reason: .

  10. #10
    Super Contributor
    Join Date
    Nov 2007
    Location
    Sertaozinho/Brazil
    Posts
    768

    Re: Problem with GPS and threads

    Quote Originally Posted by AJ____92 View Post
    i meant with local data:
    i have a class and the data is stored in the class in the mainscript
    if i call a thread and the threads function need to access a var in the object of the class it will work ?
    So, the object is global, right ? Ok, you can access any method of this global object.

    Quote Originally Posted by AJ____92 View Post
    and about the speed lets say i have 10 functions that need 1 second to get through and each of those 10 functions needs 10 % CPU
    so with threads i'll need less than 0.2 second ? if i go through all functions at the same time ?
    No! You have only one core to go, see it? If you run these threads (10% of CPU time for each one, 1s total usage) your total time will be at least slightly bigger than 1s (task switching will take some time as well). Yes, it will get slightly worst. I am supposing that all thread are busy doing their job and not waiting.

    In summary, two intensive threads will divide the CPU for these threads, there is no magic, you have only one core to execute instructions. Only in multi core processors it is possible to have a better performance since threads could be executed concurrently but not using thread module due to the Python GIL (see links I sent, please and this link about threads).

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    166

    Re: Problem with GPS and threads

    i ve tried a few things and it seems to work.

    THX

    gona add that into my engine xD
    i hope i'll gain a lil bit more speed :P

Similar Threads

  1. problem with thread
    By maclun123 in forum Symbian
    Replies: 3
    Last Post: 2008-09-25, 22:18
  2. N93i + TomTom GPS receiver problem
    By wrxch in forum Bluetooth Technology
    Replies: 2
    Last Post: 2008-05-25, 21:48

Posting Permissions

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