Struggling with threading structure

Threads (Python thread objects) cannot be cancelled in Python. There has been some talk about this (for instance Making it simpler to gracefully exit threads). Timers can be cancelled and concurrent.futures.Future objects (though that’s not guaranteed to succeed), but Thread objects basically only have start/run/join.

But your code and the code in the link is not using any threading, but asyncio. Asyncio is useful for IO-bound processes, but gives you even less control over timing then threading (since it really depends on which futures are awaitable at runtime and where the awaits and io are called - even in the simplest scenarios that quickly becomes intractable for human programmers). However, you are also not really using asyncio, you only use the eventloop… without any async functions.

I would suggest not using asyncio at all, but using a threadpool with three threads. For each of them I would profile the code first separately, to ensure that in the worst case scenario it’s all working.

The “resolution-perfect clock function” from stack overlow is imo kind of nonsense for your usecase (as @Rosuav also pointed out). And asyncio is not necessary to get “good-enough” timings (which is the only sensible thing to aim for). You might just as well use threading + time.

But why use any timers at all? If the critical function and the other functions/reporting each run in their own thread (or in their own process), and if you would always just process everything as fast as possible as soon as data becomes available, would this cause any problems? What problem are you trying to solve by adding a timer or scheduler for the critical function (or for that matter for the reporting - which I assume is just logging or DB access?)