On Tue, 10 May 2022 16:12:13 +0100 Barney Stratford <barney_stratf...@fastmail.fm> wrote: > Hello all. > > It occurs to me that creating threads in Python is more awkward than it needs > to be. Every time I want to start a new thread, I end up writing the same > thing over and over again: > > def target(*args, **kwds): > ... > t = threading.Thread(target = target, args = <something>, kwargs= <something>) > t.start() > > This becomes especially repetitive when calling a target function that only > makes sense when run in a new thread, such as a timer. > > In my own code, I’ve taken to decorating functions that always run in new > threads. Then I can call the function using the usual function call syntax, > and have the new thread returned to me. With the decorator, the code reads > instead: > > @threaded > def target(*args, **kwds): > … > t = target(*args, **kwds) > > 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 can concentrate more on > what my code does and less in how it does it. > > It feels like the right place for this decorator is the standard library, so > I’ve created PR #91878 for it. @rhettinger suggests that this is a bit > premature, and that we should discuss it here first. Thoughts?
This seems like an attractive nuisance. Creating threads comes with its own constraints and subtleties. I don't think it really helps users to hide it behind a "regular" function call. Like Greg I'm leaning towards -1 on this. Regards Antoine. _______________________________________________ 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/7XXVN7GOBWUFORMDLHQGLFIYZH23IXA2/ Code of Conduct: http://python.org/psf/codeofconduct/