[Python-Dev] Starting a new thread
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 = , kwargs= ) 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/
[Python-Dev] Re: Starting a new thread
> 1. Does t = target(...) start the thread? I think it does. I think it does too. In the commonest use case, immediately after creating a thread, you start it. And if you want to delay the thread but still use the decorator, then you can do that explicitly with some locks. In fact, it’s probably better to explicitly delay execution than have hidden requirements concerning the timing of thread creation and startup. > 2. Is it possible to create daemon threads? Not at the moment. I did think about this, but felt that simpler is better. Like you say, it’d be easy to add. In fact, I might just go ahead and add it to the PR in a bit. The simplest way to do it is probably to define a second decorator for daemonic threads. > 3. Can you create multiple threads for the same function? I assume t1, > t2, t3 = target(arg1), target(arg2), target(arg3) would work. That’s exactly what I had in mind. Make it so that thread creation and function call look exactly alike. You can call a function as many times as you want with whatever args you want, and you can create threads as often as you want with whatever args you want. There isn’t a single use case where the decorator is particularly compelling; rather, it’s syntactic sugar to hide the mechanism of thread creation so that code reads better. I could give some examples of where I used it recently if you want, but I don’t think it would be terribly illuminating. More useful might be to look at the problem from the opposite perspective: would anyone like to write result = call_function(target = foo, args = (42,), kwargs = {“bar”: 60}) in preference to result = foo(42, bar = 60) 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/YESYSRUCR666HASBXYHO2SI6AI6T5MEW/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-Dev] Re: Starting a new thread
It seems like the consensus is that this is a good idea, but it’s the wrong good idea. Should I cancel the PR or should we try to make it into a better good idea? 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/Z4AWYWNHJGOSBBU6DFV3ZS26WWQIILMW/ Code of Conduct: http://python.org/psf/codeofconduct/