Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blocking=False in consumer causes errors #57

Open
hemna opened this issue Dec 23, 2020 · 1 comment
Open

Blocking=False in consumer causes errors #57

hemna opened this issue Dec 23, 2020 · 1 comment

Comments

@hemna
Copy link

hemna commented Dec 23, 2020

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
@rossengeorgiev
Copy link
Owner

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants