0

I want to write an application that will infinitely generate events and process them in different ways. Precisely, let's say I want to measure temperature each minute and depending on that adjust climate control. I decided to use RxPY and idea is to periodically send events from one thread, but schedule their processing on other threads from a pool.

What is the best way to achieve continuous infinite process for that? My current idea is to have a good old while True: loop, but for some reason it doesn't seem right for me. Does anybody have any suggestions if this can be implemented differently?

From the main method I do this:

if __name__ == '__main__':
    store: Store = Store()
    scheduler = sched.scheduler(time.time, time.sleep)
    while True:
        scheduler.enter(5, 1, lambda: store.dispatch(measure_temperature()))
        scheduler.run()

and sometime later the following function processes these events like this:

epic_scheduler = ThreadPoolScheduler(optimal_thread_count)

def a(action):
    for epic in epics:
        epic(rx.of(action)).subscribe(lambda act: do_stuff(act),
                                      scheduler=epic_scheduler)

Something like this for main method seems more appropriate, but how do I make the main thread to not terminate right away?

if __name__ == '__main__':
    store: Store = Store()
    event_loop_scheduler = scheduler.EventLoopScheduler()
    event_loop_scheduler.schedule_periodic(5, lambda arg: store.dispatch(measure_temperature()))
Leonid Bor
  • 2,064
  • 6
  • 27
  • 47

0 Answers0