Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python.': 0.02; '(using': 0.07; 'socket': 0.07; 'happens.': 0.09; 'python': 0.11; 'def': 0.12; 'thread': 0.14; '9:15': 0.16; 'closed.': 0.16; 'distinct': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'omitting': 0.16; 'sockets': 0.16; 'subject:array': 0.16; 'thread,': 0.16; 'true:': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'normally': 0.19; 'server,': 0.19; 'creating': 0.23; "i've": 0.25; 'switch': 0.26; 'subject:/': 0.26; '(for': 0.26; 'header:In-Reply- To:1': 0.27; 'correct': 0.29; 'appreciated.': 0.29; 'am,': 0.29; 'array': 0.29; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'context,': 0.31; 'probably': 0.32; 'stuff': 0.32; 'reader': 0.33; 'maybe': 0.34; 'could': 0.34; 'connection': 0.35; 'case,': 0.35; 'more,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'google': 0.35; 'add': 0.35; 'there': 0.35; 'done': 0.36; 'so,': 0.37; 'list': 0.37; 'implement': 0.38; 'thank': 0.38; 'easiest': 0.38; 'ends': 0.38; 'to:addr:python- list': 0.38; 'that,': 0.38; 'anything': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'remove': 0.60; 'new': 0.61; 'simply': 0.61; 'simple': 0.61; "you're": 0.61; "you'll": 0.62; 'different': 0.65; 'news': 0.67; 'groups.': 0.74; 'listening': 0.74; 'replies.': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=8P6Wh6UOJjWsqOmucXOPXGPs2zM4L/G+fb/z+frmi20=; b=nI8g7v0GeiUS/hTfvyBZh6iHmPGkc9HHvYY9WCVN/euMbOLFje7dVeuds44qotKfEk f5GegELrc+9yOzX2wVMdthYvlOyucB1wpHCf1nqvNQcehymDcNCss1OWsGAobdnS9JDz h9uEAZJYnWoJQOVh/LNK9PonNxqOY3oioR0FJrfpqtUQ6e1R6tCXpa7Uo09MmxkLHgmn 7Gf4Rphz4RJf6oQdpMtyXEU7Q9fytYgP/4TvIGslb3PbUy04RUX5Fy47KBF5Q765Mok6 +pZ5w/hjrZcR2en3uZ7Ii/8U7CY9jD5m3MDZETwNTtFiuytLIWCc82SuuC6I6TIQwwN+ Uytg== MIME-Version: 1.0 X-Received: by 10.66.142.42 with SMTP id rt10mr17474033pab.1.1382826667775; Sat, 26 Oct 2013 15:31:07 -0700 (PDT) In-Reply-To: References: Date: Sun, 27 Oct 2013 09:31:07 +1100 Subject: Re: array/list of sockets From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1382826677 news.xs4all.nl 15954 [2001:888:2000:d::a6]:37617 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:57677 On Sun, Oct 27, 2013 at 9:15 AM, 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