On Thu, 3 Feb 2022 at 12:24, Cecil Westerhof via Python-list <python-list@python.org> wrote: > > Chris Angelico <ros...@gmail.com> writes: > > > On Thu, 3 Feb 2022 at 09:33, Barry <ba...@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 -- https://mail.python.org/mailman/listinfo/python-list