Waht do you think about my repeated_timer class
Chris Angelico
rosuav at gmail.com
Wed Feb 2 21:23:22 EST 2022
On Thu, 3 Feb 2022 at 12:24, Cecil Westerhof via Python-list
<python-list at python.org> wrote:
>
> Chris Angelico <rosuav at gmail.com> writes:
>
> > On Thu, 3 Feb 2022 at 09:33, Barry <barry at barrys-emacs.org> wrote:
> > (Side point: The OP's code is quite inefficient, as it creates a new
> > thread for each reiteration, but there's nothing wrong with that if
> > you're looking for something simple.)
>
> It is just something I wrote fast. How could I do this in a better way?
I'll answer your question, but first and foremost: Your code was fine,
and if something does what it's supposed to, that is the most
important. Everything else is minor.
But for other ways to do things, I would recommend creating a single
thread function and spawning a single thread to run it, and then
having that function call the target every N seconds. Also, consider
subclassing Thread rather than subclassing object (which, btw, is the
default; you don't need to say "class X(object)"), which will
automatically give your object all the methods of a timer.
If you feel like it, you could even look into ways to do things
without threads, but that would be a much bigger change :)
But remember: when your code does what it's supposed to, it is *fine*,
and doesn't need changing. I'm not saying that your code is bad :)
ChrisA
More information about the Python-list
mailing list