Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60260 > unrolled thread
| Started by | Bhanu Karthik <bhanukarthik2002@gmail.com> |
|---|---|
| First post | 2013-11-22 17:42 -0800 |
| Last post | 2013-11-22 21:41 -0800 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.python
select.select() Bhanu Karthik <bhanukarthik2002@gmail.com> - 2013-11-22 17:42 -0800
Re: select.select() Roy Smith <roy@panix.com> - 2013-11-22 21:15 -0500
Re: select.select() Bhanu Karthik <bhanukarthik2002@gmail.com> - 2013-11-22 21:43 -0800
Re: select.select() Chris Angelico <rosuav@gmail.com> - 2013-11-23 18:30 +1100
Re: select.select() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-23 02:29 +0000
Re: select.select() Bhanu Karthik <bhanukarthik2002@gmail.com> - 2013-11-22 21:41 -0800
| From | Bhanu Karthik <bhanukarthik2002@gmail.com> |
|---|---|
| Date | 2013-11-22 17:42 -0800 |
| Subject | select.select() |
| Message-ID | <b8d42424-e0ab-4595-9c87-25e5c1b53349@googlegroups.com> |
please help me.. what does the following line do? read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-11-22 21:15 -0500 |
| Message-ID | <roy-4170EF.21151022112013@news.panix.com> |
| In reply to | #60260 |
In article <b8d42424-e0ab-4595-9c87-25e5c1b53349@googlegroups.com>,
Bhanu Karthik <bhanukarthik2002@gmail.com> wrote:
> please help me.. what does the following line do?
>
> read_sockets,write_sockets,error_sockets =
> select.select(CONNECTION_LIST,[],[])
This is a little tricky.
First,read the docs at http://docs.python.org/2/library/select.html.
There's a lot of complicated stuff there, but just concentrate on the
description of the select.select() call for now.
Imagine a process which has a lot of network connections open. A great
example would be something like a MUD (Multi User Dungeon). You've got
one server process(*) and a bunch of clients which have all made TCP
connections over individual sockets.
Each client will be sending commands asynchronously, and the server
needs to handle this. You need some way to figure out which of those
sockets have something that's been sent to you (which you need to
process) and which are just sitting idle. That's where select() comes
in. It gives you a way to say, "Here's a list of sockets. Sleep until
one of them has something available for me to read, and let me know
which one."
One bit of complication is that you can also check for sockets which are
ready to be written on, and sockets which have some sort of error
condition. That's why the call returns a 3-tuple. But, for now, let's
just concentrate on reading.
Here's a very simplistic server which uses select():
import socket
import select
sock = socket.socket()
sock.bind(('localhost', 23000))
sock.listen(10)
# Accept four connections.
connections = []
for i in range(4):
s, addr = sock.accept()
print "Got connection from %s" % str(addr)
connections.append(s)
while True:
readable, _, _ = select.select(connections, [], [])
print "ready for reading: %s" % readable
for s in readable:
data = s.recv(1024)
print "Read from %s: %s" % (s, data)
You can write a little client which connects to this (I've got one I
used for testing, but I'll leave it to you to write one yourself as an
exercise). Connect four clients, and have them send some input in
random order.
Actually, this server has a bug (which you'll discover as soon as you
close one of the four connection), but it should serve to illustrate the
basic concept.
(*) I'm not sure if real MUDs are programmed this way, but it's a
plausible architecture. For simplicity sake, I'm assuming a
single-threaded server.
[toc] | [prev] | [next] | [standalone]
| From | Bhanu Karthik <bhanukarthik2002@gmail.com> |
|---|---|
| Date | 2013-11-22 21:43 -0800 |
| Message-ID | <56a4cbb0-bb85-43b8-8518-e67688afe59c@googlegroups.com> |
| In reply to | #60265 |
On Friday, 22 November 2013 18:15:10 UTC-8, Roy Smith wrote:
> In article <b8d42424-e0ab-4595-9c87-25e5c1b53349@googlegroups.com>,
>
> Bhanu Karthik <bhanukarthik2002@gmail.com> wrote:
>
>
>
> > please help me.. what does the following line do?
>
> >
>
> > read_sockets,write_sockets,error_sockets =
>
> > select.select(CONNECTION_LIST,[],[])
>
>
>
> This is a little tricky.
>
>
>
> First,read the docs at http://docs.python.org/2/library/select.html.
>
> There's a lot of complicated stuff there, but just concentrate on the
>
> description of the select.select() call for now.
>
>
>
> Imagine a process which has a lot of network connections open. A great
>
> example would be something like a MUD (Multi User Dungeon). You've got
>
> one server process(*) and a bunch of clients which have all made TCP
>
> connections over individual sockets.
>
>
>
> Each client will be sending commands asynchronously, and the server
>
> needs to handle this. You need some way to figure out which of those
>
> sockets have something that's been sent to you (which you need to
>
> process) and which are just sitting idle. That's where select() comes
>
> in. It gives you a way to say, "Here's a list of sockets. Sleep until
>
> one of them has something available for me to read, and let me know
>
> which one."
>
>
>
> One bit of complication is that you can also check for sockets which are
>
> ready to be written on, and sockets which have some sort of error
>
> condition. That's why the call returns a 3-tuple. But, for now, let's
>
> just concentrate on reading.
>
>
>
> Here's a very simplistic server which uses select():
>
>
>
> import socket
>
> import select
>
>
>
> sock = socket.socket()
>
> sock.bind(('localhost', 23000))
>
> sock.listen(10)
>
>
>
> # Accept four connections.
>
> connections = []
>
> for i in range(4):
>
> s, addr = sock.accept()
>
> print "Got connection from %s" % str(addr)
>
> connections.append(s)
>
>
>
> while True:
>
> readable, _, _ = select.select(connections, [], [])
>
> print "ready for reading: %s" % readable
>
> for s in readable:
>
> data = s.recv(1024)
>
> print "Read from %s: %s" % (s, data)
>
>
>
> You can write a little client which connects to this (I've got one I
>
> used for testing, but I'll leave it to you to write one yourself as an
>
> exercise). Connect four clients, and have them send some input in
>
> random order.
>
>
>
> Actually, this server has a bug (which you'll discover as soon as you
>
> close one of the four connection), but it should serve to illustrate the
>
> basic concept.
>
>
>
>
>
> (*) I'm not sure if real MUDs are programmed this way, but it's a
>
> plausible architecture. For simplicity sake, I'm assuming a
>
> single-threaded server.
Thank you for your reply.your reply helped me figure out concept.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-11-23 18:30 +1100 |
| Message-ID | <mailman.3073.1385191849.18130.python-list@python.org> |
| In reply to | #60265 |
On Sat, Nov 23, 2013 at 1:15 PM, Roy Smith <roy@panix.com> wrote: > (*) I'm not sure if real MUDs are programmed this way, but it's a > plausible architecture. For simplicity sake, I'm assuming a > single-threaded server. Yeah, they certainly can. That's effectively the way that I programmed the MUD kernel that we used at work (not for games, but it's still effectively a MUD), although I used async I/O facilities to abstract away the actual select calls. It's as good as the multi-threaded model (which is what I use in Minstrel Hall - every connection spawns a thread, which does blocking reads and blocking writes; simplifies the code when a command wants to delay the user, as it simply sleep()s), and can often scale to more concurrent connections, although for the bulk of servers that's not going to be an issue. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-11-23 02:29 +0000 |
| Message-ID | <529012f8$0$29993$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #60260 |
On Fri, 22 Nov 2013 17:42:07 -0800, Bhanu Karthik wrote: > please help me.. what does the following line do? > > read_sockets,write_sockets,error_sockets = > select.select(CONNECTION_LIST,[],[]) The select.select function takes three arguments (plus an optional fourth): select.select(read_list, write_list, exception_list) Each list should a list of the file descriptors you want to wait for. On Windows, only sockets are valid file descriptors. On Unix or Linux, you can use sockets, open file objects, or low-level file descriptors. In this case, you only pass CONNECTION_LIST, the others are empty lists []. CONNECTION_LIST is probably a list of sockets to be read. When they are ready for reading, select() will return three lists: read_sockets - a list of the sockets open for reading write_sockets and error_sockets should both be empty lists, since you didn't request any of those to be opened. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Bhanu Karthik <bhanukarthik2002@gmail.com> |
|---|---|
| Date | 2013-11-22 21:41 -0800 |
| Message-ID | <0d3a0c74-58eb-4cc7-853e-b0525a186ff7@googlegroups.com> |
| In reply to | #60268 |
On Friday, 22 November 2013 18:29:12 UTC-8, Steven D'Aprano wrote: > On Fri, 22 Nov 2013 17:42:07 -0800, Bhanu Karthik wrote: > > > > > please help me.. what does the following line do? > > > > > > read_sockets,write_sockets,error_sockets = > > > select.select(CONNECTION_LIST,[],[]) > > > > The select.select function takes three arguments (plus an optional > > fourth): > > > > select.select(read_list, write_list, exception_list) > > > > Each list should a list of the file descriptors you want to wait for. On > > Windows, only sockets are valid file descriptors. On Unix or Linux, you > > can use sockets, open file objects, or low-level file descriptors. > > > > In this case, you only pass CONNECTION_LIST, the others are empty lists > > []. CONNECTION_LIST is probably a list of sockets to be read. When they > > are ready for reading, select() will return three lists: > > > > read_sockets - a list of the sockets open for reading > > > > write_sockets and error_sockets should both be empty lists, since you > > didn't request any of those to be opened. > > > > > > > > -- > > Steven Thank you ,your message answered the question exactly. instead of using select.select,can we do like below? read_sockets=connection_list write_sockets=[] error_sockets=[]
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web