Python Forum
threading example - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: threading example (/thread-27027.html)



threading example - DPaul - May-23-2020

Hi,
I'm trying to put together a "threading" example, that has not academic, but practical value.
I thought of people that need to answer questions (or Multiple choices) and have eg. 10 seconds to do so.
The timer is in a separate thread.

So far so good, only I hit a problem: the console input(...) function, while waiting for a user input, seems to
block out any messages coming from the timer thread (style: 'your time is up, next question".)
They only appear after the user has entered something, even after a minute....
Indeed, when I consult the internet for such apps, authors move away from threading to entirely different methods.

Question: can anybody confirm that the console input(...) statement will block out messages coming from a 2nd thread.

PS. There is a solution, i.e. using a GUI. But that makes the example less "pure" with almost a majority
of statements not directly related to "threading".
I would prefer input(...) but maybe that is even more complicated.
If I'm wrong, I'll continue testing... :-)

thx,
Paul


RE: threading example - deanhystad - May-26-2020

I think you're not getting any bites because this is not a good application of threading. A common use of threading is to continue working while doing something that blocks. But there is an assumption that the thing that blocks will eventually stop blocking

You could use multi-processing. It is hard to cleanly kill a thread, but multi-processing has "terminate()" for doing just that. Of course using multi-processing does not make a very good threading demo.


RE: threading example - DPaul - May-26-2020

OK, thanks,

I gave up on input(...) with a timer running in a separate thread.
I turned to Tkinter, where an entry widget just sits there, waiting for input,
but commands no exclusive attention like input(...). Doing it this way also
has its quirks, but i'm seeing better results. :-)

Paul