Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #57677

Re: array/list of sockets

References <f31c18ac-ef3b-42b9-90cd-c0ef4bd80c06@googlegroups.com> <bd64d4e7-6f99-4c59-b78f-7253f2240147@googlegroups.com>
Date 2013-10-27 09:31 +1100
Subject Re: array/list of sockets
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1620.1382826677.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, Oct 27, 2013 at 9:15 AM,  <theelder777@gmail.com> wrote:
> Thank you for your quick replies. I am trying to implement a very simple multithreaded webserver in python. Is using an array of sockets (for different clients) the correct way to go?

Firstly, before you respond any more, please get off Google Groups.
You are omitting context, which makes threading (topic threading here,
distinct from threads of execution) difficult to follow. Get a news
reader like Thunderbird, or switch to email (python-list@python.org).
It will be greatly appreciated. :)

As to threading in Python... You don't normally need to maintain a
list of sockets; the easiest way would be to spin off a thread every
time a new connection happens. In pseudo-code:

def connection_thread(sock):
    while True:
        sock.recv(...)
        do stuff with what you got, maybe write to the socket

def acceptloop():
    sock = ... however you're creating your listening socket
    while True:
        newsock = sock.accept()
        create new thread, connection_thread, newsock

Each thread needs only care about its one socket.

Now, if you're creating a chat server, where anything that comes in on
one socket goes to all the others, then you'll need to maintain that
list (that's why I said "normally"). In that case, the easiest way
would probably be for connection_thread to register its socket when it
begins, and unregister it when it ends (using try/finally to help).

There are other ways to manage multiple sockets. You could maintain a
list of them all and react any time one is readable/writable, with
select(); I'm not sure how to do that in Python as I've never done so,
but there's a way. With that, you'd simply add sockets to the list
when accept() has something, and remove them when they're closed. But
you said you were threading.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

array/list of sockets theelder777@gmail.com - 2013-10-26 14:41 -0700
  Re: array/list of sockets Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-26 23:01 +0100
  Re: array/list of sockets Johannes Findeisen <mailman@hanez.org> - 2013-10-26 23:59 +0200
  Re: array/list of sockets theelder777@gmail.com - 2013-10-26 15:15 -0700
    Re: array/list of sockets Chris Angelico <rosuav@gmail.com> - 2013-10-27 09:31 +1100
    Re: array/list of sockets Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-26 23:33 +0100
  Re: array/list of sockets theelder777@gmail.com - 2013-10-26 15:42 -0700
    Re: array/list of sockets Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-27 00:03 +0100
      Re: array/list of sockets theelder777@gmail.com - 2013-10-26 16:17 -0700
        Re: array/list of sockets mm0fmf <none@mailinator.com> - 2013-10-27 00:26 +0100
        Re: array/list of sockets Chris Angelico <rosuav@gmail.com> - 2013-10-27 10:27 +1100
        Re: array/list of sockets Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-27 00:36 +0100
          Re: array/list of sockets theelder777@gmail.com - 2013-10-26 16:56 -0700
            Re: array/list of sockets Chris Angelico <rosuav@gmail.com> - 2013-10-27 11:05 +1100
        Re: array/list of sockets rurpy@yahoo.com - 2013-10-26 21:29 -0700
        Re: array/list of sockets rusi <rustompmody@gmail.com> - 2013-10-27 01:53 -0700

csiph-web