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?

Cheers,
Barney.
_______________________________________________
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/Z6W56AQHBAA7PJ3HAUK6YQNMOJO27V6Z/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to