473,383 Members | 1,788 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

threads and sockets

hi!

how can i stop a server socket running in a thread other than the main
thread? if the server socket was a local variable of the function started
by the child thread, would calling join work?

thanks


----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
Jul 18 '05 #1
3 1690
Ajay wrote:
how can i stop a server socket running in a thread other than the main
thread? if the server socket was a local variable of the function started
by the child thread, would calling join work?


Short answer: you can't, and join won't help. Use select on sockets, with a
timeout, or use twisted.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
how can i stop a server socket running in a thread other than the main
thread? if the server socket was a local variable of the function started
by the child thread, would calling join work?


Short answer: you can't, and join won't help. Use select on sockets, with a
timeout, or use twisted.


More specifically:

def serverthread(sock):
while not quit:
try:
select(sock, sock, sock, 1)
except:
#handle errors, but don't use a bare except in real code
#close sock if necessary

- Josiah

Jul 18 '05 #3
Ajay <ab******@mail.usyd.edu.au> wrote in message news:<ma**************************************@pyt hon.org>...
hi!

how can i stop a server socket running in a thread other than the main
thread? if the server socket was a local variable of the function started
by the child thread, would calling join work?


Here is how you do what this:

import socket, threading

class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
HOST = ''
PORT = 50007
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#! blocking operatioms on this socket (accept) will timeout
self.s.settimeout(1)
self.s.bind((HOST, PORT))
self.s.listen(5)
self._stop = False
def stop(self):
self._stop = True
def run(self):
while 1:
try:
if self._stop:
print "stop requested"
break
conn, addr = self.s.accept()
print "connection from", addr
except socket.timeout:
print "timeout"
except socket.error:
print "socket.error"
break
self.s.close()

def TimerFucn(t):
print "TimerFucn"
t.stop()
# you can do t.close(). accept() will throw an exception
# in thos case all the stuff with stop() is not needed

listener = MyThread()
t = threading.Timer(10, TimerFucn, (listener,))
t.start()
listener.start()

while 1:
s = raw_input()
if s[0] == 'q':
break
Jul 18 '05 #4

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

Similar topics

4
by: Julia Goolia | last post by:
hello, i read that it is bad to use threads with tkinter. so my question is how does one create a gui program with sockets? at one point you have to call mainloop() which does not return. ...
0
by: Gonçalo Rodrigues | last post by:
Hi, I have a problem with threads and sockets. I'll try to describe the problem in words with pseudo-code. I've been working on a few classes to make it easier to work with threads. This...
6
by: Zunbeltz Izaola | last post by:
Hi, I have the following problem. I'm developing a GUI program (wxPython). This program has to comunicate (TCP) whit other program that controls a laboratory machine to do a measurement. I...
5
by: Russell Warren | last post by:
Does anyone know the scope of the socket.setdefaulttimeout call? Is it a cross-process/system setting or does it stay local in the application in which it is called? I've been testing this and...
2
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If...
2
by: Ronodev.Sen | last post by:
the way my program needs to go is -- 1) open a socket and listen on it 2) moment a client connects to the socket - process some data (by sending it to another machine), get the result and send...
4
by: nyhetsgrupper | last post by:
Hi, I've written a async server app. This app start by connecting to a client and then send some data (BeginSend). When the data is sent, the server is starting to listen for incomming data....
6
by: roblugt | last post by:
I have what I imagine is a well-known .Net networking problem, but even though I've Googled for some time I've not yet come across a thread where this has been fully explained... There is a...
2
by: jgbid | last post by:
Hi, I'm trying to build an IP Scanner inc c# for a specific port (80) and for specific IP Ranges. For example 24.36.148.1 to 24.36.148.255 My first step was to use TcpClient, but there are...
0
by: Qui Sum | last post by:
I write ip/port scanner so users using search on our LAN know if the particular ftp is online or not. The class I write has the following structure: public class IPScanner { public...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.