select.select()

B

Bhanu Karthik

please help me.. what does the following line do?

read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])
 
R

Roy Smith

Bhanu Karthik said:
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.
 
S

Steven D'Aprano

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

Bhanu Karthik

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.

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=[]
 
B

Bhanu Karthik

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

Chris Angelico

(*) 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,215
Messages
2,571,113
Members
47,708
Latest member
SharonMaes

Latest Threads

Top