Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

threads and sockets

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


abra9823 at mail

Oct 13, 2004, 4:14 AM

Post #1 of 8 (200 views)
Permalink
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.
--
http://mail.python.org/mailman/listinfo/python-list


deetsNOSPAM at web

Oct 13, 2004, 5:27 AM

Post #2 of 8 (199 views)
Permalink
Re: threads and sockets [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


jcarlson at uci

Oct 13, 2004, 11:49 AM

Post #3 of 8 (198 views)
Permalink
Re: threads and sockets [In reply to]

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

--
http://mail.python.org/mailman/listinfo/python-list


elbertlev at hotmail

Oct 13, 2004, 7:32 PM

Post #4 of 8 (200 views)
Permalink
Re: threads and sockets [In reply to]

Ajay <abra9823 [at] mail> wrote in message news:<mailman.4826.1097666075.5135.python-list [at] python>...
> 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
--
http://mail.python.org/mailman/listinfo/python-list


dieter at handshake

Aug 10, 2012, 9:38 AM

Post #5 of 8 (122 views)
Permalink
Re: Threads and sockets [In reply to]

loial <jldunn2000 [at] gmail> writes:

> I am writing an application to send data to a printer port(9100) and then recieve PJL responses back on that port. Because of the way PJL works I have to do both in the same process(script).
>
> At the moment I do not start to read responses until the data has been sent to the printer. However it seems I am missing some responses from the printer whilst sending the data, so I need to be able to do the 2 things at the same time.
>
> Can I open a port once and then use 2 different threads, one to write to the post and one to read the responses)?

That should be possible. Alternatively, you could use "asyncore" -- a
mini framework to facilitate asynchronous communication.

--
http://mail.python.org/mailman/listinfo/python-list


drsalists at gmail

Aug 10, 2012, 11:43 AM

Post #6 of 8 (120 views)
Permalink
Re: Threads and sockets [In reply to]

A select() loop should work.

On Fri, Aug 10, 2012 at 1:01 PM, loial <jldunn2000 [at] gmail> wrote:

> I am writing an application to send data to a printer port(9100) and then
> recieve PJL responses back on that port. Because of the way PJL works I
> have to do both in the same process(script).
>
> At the moment I do not start to read responses until the data has been
> sent to the printer. However it seems I am missing some responses from the
> printer whilst sending the data, so I need to be able to do the 2 things at
> the same time.
>
> Can I open a port once and then use 2 different threads, one to write to
> the post and one to read the responses)?
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


invalid at invalid

Aug 10, 2012, 3:38 PM

Post #7 of 8 (124 views)
Permalink
Re: Threads and sockets [In reply to]

On 2012-08-10, loial <jldunn2000 [at] gmail> wrote:

> At the moment I do not start to read responses until the data has
> been sent to the printer. However it seems I am missing some
> responses from the printer whilst sending the data, so I need to be
> able to do the 2 things at the same time.
>
> Can I open a port once and then use 2 different threads, one to write
> to the post and one to read the responses)?

By "port" I assume you mean a TCP connection using the 'socket' module?

If so, then yes you can write using one thread and read using a
second thread. I do that all the time.

Sometimes it's simpler to use a single thread that uses select or
poll, and sometimes it's simpler to use multiple threads. And you
never know which way is best until you're half way down the wrong
road...


--
http://mail.python.org/mailman/listinfo/python-list


ulrich.eckhardt at dominolaser

Aug 13, 2012, 12:15 AM

Post #8 of 8 (116 views)
Permalink
Re: Threads and sockets [In reply to]

Am 10.08.2012 15:01, schrieb loial:
> I am writing an application to send data to a printer port(9100) and
> then recieve PJL responses back on that port. Because of the way PJL
> works I have to do both in the same process(script).

If I understand that right, you are opening a TCP connection, so
obviously this must be done in the same process, regardless of what PJL
(whatever that exactly is) does.


> At the moment I do not start to read responses until the data has
> been sent to the printer. However it seems I am missing some
> responses from the printer whilst sending the data, so I need to be
> able to do the 2 things at the same time.

Using TCP, that shouldn't happen, so I really wonder what exactly you
are doing here.


> Can I open a port once and then use 2 different threads, one to write
> to the post and one to read the responses)?

Yes, definitely, take a look at the select() function of the select
module. This basically looks like this:

(r, w, x) = select(...)
if r:
# read and handle incoming data
...
if w:
# write pending output data
...
if x:
# handle connection failure
...


If all this is not what you are doing and what you want (which I'm not
100% sure of) then please elaborate a bit what you're doing and what
kind of connection you are using.

Happy hacking!

Uli
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.