Skip to content

Commit

Permalink
Ctrl+C support for mode-s
Browse files Browse the repository at this point in the history
Signed-off-by: Clemens Vasters <clemens@vasters.com>
  • Loading branch information
clemensv committed Jan 22, 2025
1 parent a86b6b4 commit 6f0651d
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions mode-s/mode_s/mode_s.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import sys
import asyncio
import threading
import aiohttp
import re
import logging
Expand Down Expand Up @@ -154,12 +155,16 @@ async def queue_consumer(self):
self.messages_since_last_flush = 0
self.records_since_last_flush = 0
last_flush = datetime.now()
except asyncio.CancelledError:
logging.info("Queue consumer task cancelled")
return
except Exception as e:
logging.error("Error sending messages: %s", e)
else:
await asyncio.sleep(0.05)
except asyncio.CancelledError:
logging.info("Queue consumer task cancelled")
return
except Exception as e:
logging.error("Queue consumer task error: %s", e)
raise e
Expand Down Expand Up @@ -286,11 +291,22 @@ async def main():
ref_lon=args.ref_lon,
stationid=args.stationid
)
tasks = [
asyncio.create_task(client.queue_consumer()),
asyncio.to_thread(client.run)
]
await asyncio.gather(*tasks)
try:
stop = False
# Run client.run as a regular thread
run_thread = threading.Thread(target=client.run, kwargs={'stop_flag': stop})
run_thread.start()

await asyncio.create_task(client.queue_consumer())
stop = True
client.stop()
# Wait for the thread to finish
run_thread.join()

except KeyboardInterrupt:
print("Interrupted")
except Exception as e:
print("Error: %s" % e)


if __name__ == "__main__":
Expand Down

0 comments on commit 6f0651d

Please sign in to comment.