Chat system

Cliff Crawford cjc26 at nospam.cornell.edu
Thu Mar 16 03:49:39 CET 2000


Pada Wed, 15 Mar 2000 15:36:04 -0500, Chris Armstrong bilang:
| Another thing about the chat system: I need for it to be able to constantly
| stream in new content while still being able to type in your message and
| send it whenever you want. I know some programs like IRC clients do
| this without having two processes (one for the streaming content and
| one for the input). Oh, and one more thing, this'll all be using curses,
| if it matters.

You can do this using select() (in the "select" module :).  You pass
select three lists of file descriptors that you're waiting to do
something with.  One list is for files you're waiting to read from,
another is for files you want to write to, and the third is for error
conditions.  Select blocks until one or more of those file
descriptors are actually ready, or an error occurs on the one of the
files you are watching for errors.  You can also set a timeout if you
need to.

So, for what you want to do, you would call select inside an infinite
loop.  The arguments would be the socket you are listening to, and
the file descriptor for stdin.  Thus either data coming in on the
socket, or a user pressing a key, will cause select to return the
appropriate file desc.; from there your program can figure out what
to do next.  So you would do something like:

	soc=socket.fileno()
	in=sys.stdin.fileno()
	while 1:
		r, w, e = select([soc, in], [], [soc, in])
		for fd in r:
			# process input

Also, there's the asyncore module, which is a higher-level interface
to all of this.


-- 
cliff crawford    -><-    http://www.people.cornell.edu/pages/cjc26/
"IS I yes wardrobe yield [the] evaluation."     member of A.H.M.A.D.



More information about the Python-list mailing list