aprs-python icon indicating copy to clipboard operation
aprs-python copied to clipboard

Blocking=False in consumer causes errors

Open hemna opened this issue 3 years ago • 1 comments

I am trying to create a threaded app that can stop each thread upon CTRL-C, and would like to set a timeout on consumer select.select to check if the app should quit or not. When I set blocking=False in consumer setup, I get nothing but resource not available errors.

Source:

I edited the source to not include my callsign and password

import aprslib
import logging

def callback(packet):
    try:
        packet = aprslib.parse(packet)
        print(packet)
    except (aprslib.ParseError, aprslib.UnknownFormat) as exp:
        pass

logging.basicConfig(level=logging.INFO) # level=10

AIS = aprslib.IS("<MY ACCOUNT HERE>", passwd="<MY PASSWORD HERE>", port=14580, host="rotate.aprs2.net")
AIS.connect()
AIS.consumer(callback, raw=True, blocking=False, immortal=True)

The output

INFO:aprslib.inet.IS:Attempting connection to rotate.aprs2.net:14580
INFO:aprslib.inet.IS:Connected to ('62.77.224.245', 14580)
INFO:aprslib.inet.IS:Sending login information
INFO:aprslib.inet.IS:Login successful
ERROR:aprslib.inet.IS:socket error on recv(): [Errno 11] Resource temporarily unavailable

hemna avatar Dec 23 '20 18:12 hemna

The error logged is when attempting to read from socket that has not received anything yet. That a result of setting blocking=False. The call returns immediately, or processes any packets that are available. Since you just connected, you have not yet received any packets. You will have to repeatedly make the consumer call to receive packets, and implement your own loop and wait logic.

14580 port is for user defined filter. I don't see one defined. I think it's packets addressed to login callsign or something. I use AIS.set_filter('t/poimqstunw') to receive all packet types.

You can use https://docs.python.org/3/library/threading.html#threading.Event to communicate to the thread that the program is exiting. Here is rough example:

def worker(con, exit_event):
    while not exit_event.is_set():
        con.consumer(callback, raw=True, blocking=False, immortal=True)
        time.sleep(0.5)

exit_event = threading.Event()

con1 = ...
con2 = ...

t1 = threading.Thread(target=worker, args=(con1, exit_event))
t2 = threading.Thread(target=worker, args=(con2, exit_event))

# catch keyboard interrupt, or signal
exit_event.set()

t1.join()
t2.join()

rossengeorgiev avatar Dec 20 '21 18:12 rossengeorgiev