Reputation: 44361
I need the following architecture:
scheduling-thread(S):
- push scheduling event to "schedule" queue, with `data` and a `deadline`
scheduler-thread:
- forever loop
- process scheduling events from the "schedule" queue
- push event to a "deadlines-met" queue when deadline is met
customer-thread(S):
- listen to "deadlines-met" queue
That is, the scheduler-thread receives data from scheduling-threads via the "schedule" queue, and pushes them to a "deadlines-met" queue whenever the deadline is met.
Clients listening on the "deadlines-met" queue will receive the events at the desired time.
I am worried that the implementation of scheduler-thread
can be complicated, since it needs to do two things:
And both can not be done at the same time: that is, if I am waiting for a deadline to expire, I can not listen for new scheduling events, and if I am listening I can not wait for a deadline to expire.
How could I implement this scheduling thread? The easy alternative (sched module), would block my thread while waiting for deadlines to expire, so that I could not process new scheduling events.
Upvotes: 0
Views: 2786
Reputation: 39354
The other way to do this is with a priority queue.
I've not done this in Python, but the idea is to keep the deadlines sorted and to wait for the shortest. If you use a condition object you can wait()
for the shortest, but when another event is posted, then a notify()
will cancel the sleep and the thread places the next event into the sorted list and again waits for the shortest.
Upvotes: 1
Reputation: 34307
make the queue object in your "main" program
start both threads with Threading.thread from the "main" program
check the queues from each thread
You could block on read from the queues or you could sleep and check every second with Queue.empty()
See this example How to use threading in Python?
Upvotes: 0