Interesting Thread Gotcha

Diez B. Roggisch deets at nospam.web.de
Wed Jan 16 14:28:47 EST 2008


Dan schrieb:
> On Jan 16, 1:33 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>> Dan schrieb:
>>
>>
>>
>>> On Jan 16, 11:06 am, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>>>> Hendrik van Rooyen wrote:
>>>>> "Dan" <the,,,ail.com> wrote:
>>>>>>>>> keyboard_thread = thread.start_new_thread(kbd_driver (port_q,kbd_q))
>>>>>> Needs to be
>>>>>>>>> keyboard_thread = thread.start_new_thread(kbd_driver, (port_q,kbd_q))
>>>>>> Commas are important!
>>>>>> -Dan
>>>>> Absolutely! - well spotted!
>>>>> As the first correct respondent, you win the freedom to spend a week in
>>>>> Naboomspruit at your own expense.
>>>>> It would have been nice, however, to have gotten something like:
>>>>> TypeError - This routine needs a tuple.
>>>>> instead of the silent in line calling of the routine in question,
>>>>> while failing actually to start a new thread.
>>>> You can't prevent the silent inline-calling - otherwise, how would you do
>>>> this:
>>>> def compute_thread_target():
>>>>     def target():
>>>>         pass
>>>>     return target
>>>> thread.start_new_thread(compute_thread_target())
>>>> Of course start_new_thread could throw an error if it got nothing callable
>>>> as first argument. No idea why it doesn't.
>>>> Diez
>>> Of course, in his case, having start_new_thread throw an error
>>> wouldn't have helped, since he went into an infinite loop while
>>> evaluating the parameters for start_new_thread.
>>> Would it be possible to have pychecker (or some such) warn that there
>>> is an insufficient parameter count to start_new_thread? I guess that
>>> would require knowing the type of thread. . .
>> What has this to do with the second argument? It's perfectly legal to
>> have a function as thread-target that takes no arguments at all, so
>> enforcing a second argument wouldn't be helpful - all it would do is to
>> force all developers that don't need an argument tuple to pass the empty
>> tuple. So there was no insufficient argument count.
>>
>> And none of these would solve the underlying problem that in python
>> expressions are evaluated eagerly. Changing that would mean that you end
>> up with a totally new language.
>>
>> the only thing that could help to a certain extend would be static
>> types. Which we don't want here :)
>>
>> Diez
> 
> It doesn't seem to be legal in my version of python (or the doc):
> 
>>>> import thread
>>>> def bat():
> 	print "hello"
> 
> 
>>>> thread.start_new_thread(bat)
> 
> Traceback (most recent call last):
>   File "<pyshell#12>", line 1, in <module>
>     thread.start_new_thread(bat)
> TypeError: start_new_thread expected at least 2 arguments, got 1
>>>> thread.start_new_thread(bat, ())
> 2256hello

Ah, I thought it was optional, as in the threading.Thread(target=..., 
args=....)-version. Sorry for not looking that up.

Then you'd might stand a chance that pychecker can find such a situation 
- but of course not on a general level, as in the above - that would 
only work with type-annotations.



Diez



More information about the Python-list mailing list