Skip to content

Bad example - 100% CPU due to looping #26

@regulatre

Description

@regulatre

The sample code below is currently returned as the top search result for python gps. It's hosted at this URL https://pypi.org/project/gps3/ and the big problem here is that the loop runs out of control, trashing the CPU usage for no reason. I would recommend adding a sleep to the loop. Quite simply a "time.sleep(0.2)" just below the "for new_data in ..." line would save a ton of carbon as N00bs using the sample code don't end up with code that max's out their CPU with useless looping.

from gps3 import gps3
gps_socket = gps3.GPSDSocket()
data_stream = gps3.DataStream()
gps_socket.connect()
gps_socket.watch()
for new_data in gps_socket:
    if new_data:
        data_stream.unpack(new_data)
        print('Altitude = ', data_stream.TPV['alt'])
        print('Latitude = ', data_stream.TPV['lat'])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions