By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,766 Members | 1,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,766 IT Pros & Developers. It's quick & easy.

A beginner's question on thread

P: n/a
I'm writing a small utility that listens for socket connections, and
also repond to user inputs via a text menu selection.

In order to respond to both the user and the incoming connections I
figure I need to use thread.

I think I would use the main thread to handle the menu and spawn a
thread which listens on a socket.

Now my question is, how do I terminate the server thread if user wants
to exit the application? If the server just sit there and listen the
thread would never terminate by itself. What kind of inter-thread
communications are available and where would I find info/tutorial on that?

Timothy

Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a


Timothy Wu wrote:
I'm writing a small utility that listens for socket connections, and
also repond to user inputs via a text menu selection.

In order to respond to both the user and the incoming connections I
figure I need to use thread.

I think I would use the main thread to handle the menu and spawn a
thread which listens on a socket.

Now my question is, how do I terminate the server thread if user wants
to exit the application? If the server just sit there and listen the
thread would never terminate by itself. What kind of inter-thread
communications are available and where would I find info/tutorial on that?

Timothy


Well, when the user wants to exit, the main thread could open a socket
connection to the server thread, and send a special message telling it
to stop.

The server thread would have to distinguish between normal connections
from clients and special connections from the main thread.

Hope this helps

Aurelio
Jul 18 '05 #2

P: n/a
Aurelio Martin wrote:
Well, when the user wants to exit, the main thread could open a socket
connection to the server thread, and send a special message telling it
to stop.

The server thread would have to distinguish between normal connections
from clients and special connections from the main thread.

Hope this helps

Aurelio


I've thought about doing that and I thought that was more of a hack.
Is it the proper way to handle it?

Timothy

Jul 18 '05 #3

P: n/a
> Now my question is, how do I terminate the server thread if user wants
to exit the application?


The easiest way is to setDaemon(True) on the thread. See the doc for
exactly what it does.

If you want to do it in a more "proper" fashion, then you have to
make the thread do a polling loop, such as by putting a short
timeout on the sockets, and then check a variable which indicates
if you should continue, looping back if not.

It would be really nice if Python supported interrupting a thread
like Java does.

Roger
Jul 18 '05 #4

P: n/a
Timothy Wu wrote:
I'm writing a small utility that listens for socket connections, and
also repond to user inputs via a text menu selection.

In order to respond to both the user and the incoming connections I
figure I need to use thread.

I think I would use the main thread to handle the menu and spawn a
thread which listens on a socket.

Now my question is, how do I terminate the server thread if user wants
to exit the application? If the server just sit there and listen the
thread would never terminate by itself. What kind of inter-thread
communications are available and where would I find info/tutorial on that?

No need for threads.

If you have your server set up properly with asyncore, the below link
will give you an example using wxPython that will just work...

http://groups.google.com/groups?hl=e....lang.python.*

Enjoy.

- Josiah
Jul 18 '05 #5

P: n/a
Quoth Timothy Wu <hu**********@graffiti.idv.tw>:
| I'm writing a small utility that listens for socket connections, and
| also repond to user inputs via a text menu selection.
|
| In order to respond to both the user and the incoming connections I
| figure I need to use thread.
|
| I think I would use the main thread to handle the menu and spawn a
| thread which listens on a socket.
|
| Now my question is, how do I terminate the server thread if user wants
| to exit the application? If the server just sit there and listen the
| thread would never terminate by itself. What kind of inter-thread
| communications are available and where would I find info/tutorial on that?

Well, that's the deal. Use threads to solve a problem, and now
you have two problems.

I'd probably do it the way Aurelio Martin proposed, with I/O to
the socket thread. It's much neater than whacking the thread,
which is the alternative.

I wouldn't necessarily use threads in the first place, though.
I think it really depends on the user interface - usually that
has to be the dispatch core of the application, and if it's
built for threads, then there you go. Typically there will be
some provision for dispatch on an I/O channel like a socket.
If you're writing your own user interface, probably just reading
and writing to the tty, and the socket thread is going to be
the one and lonely extra thread - then don't do it, I'd say.
Use select.select, or a package like asyncore or twisted or
whatever (not very familiar with that territory, I just use select.)

Donn Cave, do**@drizzle.com
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.