0

I have a set of data like

schedule = [(2, 5),
            (4, 6),
            (10, 2)]

with the first element of each tuple being a time (in seconds) and the second element being a value.

I would like to start a separate thread which tracks the time and at each scheduled time runs some arbitrary function func(value).

What is the cleanest way to do this? I could create a Timer object for each scheduled time, but that seems sloppy.

edit: You can assume the times are in ascending order

4
  • Are the times necessarily in ascending order? Commented Aug 4, 2016 at 3:05
  • Yes, I'm sorting them from the start, but if there is a robust solution that doesn't make the assumption that they are sorted, that is great too Commented Aug 4, 2016 at 3:07
  • Probably overkill but if this is for a big project or might grow, consider APScheduler Commented Aug 4, 2016 at 3:12
  • Yeah, this is a small part of a small project that probably wont go anywhere Commented Aug 4, 2016 at 3:18

1 Answer 1

1

If you need them to be in separate threads then a Timer object or as a commenter pointed out APScheduler. If you want to do the threading manually you could also use sched. They added support for threads in version 3.3. Docs

Sign up to request clarification or add additional context in comments.

1 Comment

sched (in a separate thread) worked perfectly here. Just what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.