473,778 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A beginner's question on thread

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
5 1639


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
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
> 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
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
Quoth Timothy Wu <hu**********@g raffiti.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.co m
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2032
by: Fabio Papa | last post by:
I am trying to write a windows service that sends emails to clients at specific times based on information in a sql db. Since this is done for multiple cities, I start a thread for each city and continue the processing from each thread. My service starts fine (gives me no errors, etc), but it doesn't seem to start the new threads. I am new to windows services, so I don't know if I'm doing something wrong. Should I maybe be doing this...
1
2870
by: LRW | last post by:
I was wondering if anyone could recommend some good beginner sites and tutorial sites for writting ASP.Net pages in C#. Things that especially help with datagrids!! And, are there additional newsgroups that focus on C# and ASP.Net? Thanks!! Liam
8
7599
by: Shamrokk | last post by:
My application has a loop that needs to run every 2 seconds or so. To acomplish this I used... "Thread.Sleep(2000);" When I run the program it runs fine. Once I press the button that starts the looping function the window becomes unmovable and cannot close under its own direction (the upper right "close 'X'") My first attempt to solve the problem was to have the looping function execute as its own thread, the idea being this would...
5
2040
by: Robert W. | last post by:
I'm creating a WinForms app that will act as a companion (think administrator functionality) to a Pocket PC app. Generally the WinForms app works under just the UI thread. But if a Pocket PC connects to the desktop via ActiveSync then a separate thread is spawned. In addition to the main desktop app window I also have a Notification form that appears in the lower right of the screen and provides feedback to the user about what is...
0
850
by: Flashster | last post by:
I have a thread in my Windows app which uses Net.WebClient to post data to a web form and receive the resultant web page in a string. This process can take some time, and this is why I chose to use a secondary thread to stop the user interface freezing. However, I am not sure how I am supposed to cancel the thread should the user click the cancel button (or try to close the application whilst the thread is running). Is it not advisable...
3
1213
by: Gigs_ | last post by:
can someone explain me this code: ---------------------------------- import thread stdoutmutex = thread.allocate_lock() exitmutexes = * 10 def counter(myId, count): for i in range(count): stdoutmutex.acquire() print ' =%s' % (myId, i)
0
9628
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10292
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.