Thinking about it, it's actually a fine design to have a decorator that turns a regular function into one that starts a thread -- from the caller's POV it's no different than having a function that explicitly starts a thread, and it could be a nice shorthand if you do this all the time. (You have to document it in either case.)
That said, I don't think we need this 3-line decorator in the stdlib. --Guido On Thu, May 12, 2022 at 10:40 PM Serhiy Storchaka <storch...@gmail.com> wrote: > 10.05.22 18:12, Barney Stratford пише: > > This has a couple of advantages. I don’t have to import the threading > module all over my code. I can use the neater syntax of function calls. The > function’s definition makes it clear it’s returning a new thread since it’s > decorated. It gets the plumbing out of the way so I an concentrate more on > what my code does and less in how it does it. > > I do not see advantages. You definitely need to import the threading > module or other module depending on the threading module in which you > define the decorator. And you need to decorate the function. It will not > save you a line of code. > > If you need to run a lot of functions in threads, note that a thread has > some starting cost. Consider using concurrent.futures.ThreadPoolExecutor. > > If you only run few long living threads, the syntax of starting them > does not matter, in comparison with the rest of your code. > > Also, it looks wrong to me to mix two different things: what code to > execute and how to run it. If we need a neater syntax, I would propose: > > t = threading.start_thread(func, *args, **kwargs) > > But I am not sure that it is worth to add such three-line function in > the stdlib. > > _______________________________________________ > Python-Dev mailing list -- python-dev@python.org > To unsubscribe send an email to python-dev-le...@python.org > https://mail.python.org/mailman3/lists/python-dev.python.org/ > Message archived at > https://mail.python.org/archives/list/python-dev@python.org/message/RBIAWP2J3NTYMLIS3W6CKX44G5QIBXAU/ > Code of Conduct: http://python.org/psf/codeofconduct/ > -- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/DWPTNN2SZAPEP3HIAVAQ24BUYEDJSSF4/ Code of Conduct: http://python.org/psf/codeofconduct/