Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Reason for not allowing import twice but allowing reload() Date: Wed, 2 Mar 2016 05:04:39 +1100 Lines: 59 Message-ID: References: <645cdd46-d4a4-49b3-a0d8-848608d70d73@googlegroups.com> <56d57aa4$0$1586$c3e8da3$5496439d@news.astraweb.com> <56d5cd36$0$22142$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 0oK3bH0vUhL+dWfHCSYuvQNFvxIa9+93m8reuYQBr07w== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'modify': 0.04; 'startup': 0.05; 'that?': 0.05; 'imply': 0.07; 'modifying': 0.07; 'cc:addr:python-list': 0.09; 'back-end': 0.09; 'subject:()': 0.09; 'python': 0.10; 'subject:not': 0.11; 'wed,': 0.15; 'commit': 0.15; 'file,': 0.15; 'server,': 0.15; "(it's": 0.16; '2016': 0.16; 'add,': 0.16; 'async': 0.16; 'debugged': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hits': 0.16; 'initializes': 0.16; 'modules,': 0.16; 'only)': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reload': 0.16; 'subject:import': 0.16; 'unload': 0.16; 'wrote:': 0.16; 'basically': 0.18; 'debugging': 0.18; '(in': 0.18; 'creates': 0.18; 'gui': 0.18; '>>>': 0.20; 'changes': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'handles': 0.20; 'work,': 0.21; 'setup,': 0.22; 'file.': 0.22; 'am,': 0.23; 'code,': 0.23; 'code.': 0.23; 'replacing': 0.23; 'this:': 0.23; 'import': 0.24; 'implemented': 0.24; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; "i've": 0.25; "doesn't": 0.26; 'chris': 0.26; 'handling': 0.27; 'logging': 0.27; 'message-id:@mail.gmail.com': 0.27; 'data,': 0.27; 'about.': 0.29; 'loop,': 0.29; "people's": 0.29; 'restart': 0.29; 'sure,': 0.29; 'starts': 0.29; "i'm": 0.30; 'system,': 0.30; 'code': 0.30; 'probably': 0.31; 'anyone': 0.32; "can't": 0.32; 'implement': 0.32; 'possibly': 0.32; 'maybe': 0.33; "d'aprano": 0.33; 'steven': 0.33; "i'll": 0.33; 'tue,': 0.34; 'file': 0.34; 'except': 0.34; 'running': 0.34; 'add': 0.34; 'server': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'done': 0.35; 'files,': 0.35; 'quite': 0.35; 'sometimes': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'basic': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'client': 0.37; 'received:209.85.213': 0.37; "won't": 0.38; 'received:209': 0.38; 'mean': 0.38; 'end': 0.39; 'why': 0.39; 'does': 0.39; 'system.': 0.39; 'rather': 0.39; 'some': 0.40; 'your': 0.60; 'yes': 0.62; 'more': 0.63; 'mar': 0.65; 'wanting': 0.66; 'talking': 0.67; 'production': 0.67; 'satisfied': 0.83; 'chrisa': 0.84; 'interaction.': 0.84; 'uptime': 0.84; 'window,': 0.84; 'to:none': 0.91; 'crucial': 0.91; 'drops': 0.91; 'maybe,': 0.91; 'thing,': 0.93; 'insane': 0.95; 'imagine': 0.96 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:cc; bh=cOUDLJs28KX9887PunSUwh0mDmNGK4QO26i7CHL3NXs=; b=IOVQTzFDPYFPC7id0Okj1shI/Yo2NkD7ge1GZgLxJGzWYeKKiz6y27WQJMco3vSDfU RAPJl13ieZaB8pqwxckJodTzz1BqNJ50HGxsmlsQvIs4gEvdkwaHsb7Xi8zU6/4Y7H1d GWVn1owSaBSHXmchTZSAFuh04aM9KzNRDW2yoTm5I0BZvx/5vRScnKbTGYClw6CGyuqL 7FS59KlRrQEwZeX54BCs5CAM3AHXhHKgOzdliT9c7fK96is8LijXqGuqJkoDFSc37ho7 GTUl8G7+ysobFmFLarITOgOcf74LL0xgeRG7hNdV3Bc3odraSsa1j+fwDD13IhhNyKH1 tLQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=cOUDLJs28KX9887PunSUwh0mDmNGK4QO26i7CHL3NXs=; b=KPnLNgds1zuIZPoycqUQsO/jwMKV4/NeMJG2Hc2Kwqq67oLkJlpui11W0l0QGHS581 VhW6AUeEBChSLLOGsasP5n1Iz2zNFn9+//7qd0UAZWeW7JbZFgrSPxyUp3i2fptgd4KO VKQ1fEV4unJLaoA3trok/yFzGnRIkeaDlL9FSzKLylpV6GI0Osu5sbOo24KSRpE3GyrG 17KvE2HzJclVbT+GNaiufkPVrnMjv2lKZ0AKqFSICjG9gezQ5Q+t4DmMBEFttJuyTEkM pmdA37uQr19eAhNoH1HqAXxIT1MZSAnn0GlI4d0Zq0ZkQOqDy5PrMk2/ZBxU9L6JTgZM 4L1Q== X-Gm-Message-State: AD7BkJK9+rfgisHumD4AcfVIfakbGqmEuOi3/OW+ECye5d2uU9nz4ehLncsidG99xTLHVI1nP5wolR/Zb9XMJg== X-Received: by 10.50.180.35 with SMTP id dl3mr383656igc.94.1456855479336; Tue, 01 Mar 2016 10:04:39 -0800 (PST) In-Reply-To: <56d5cd36$0$22142$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103808 On Wed, Mar 2, 2016 at 4:11 AM, Steven D'Aprano wrote: > On Tue, 1 Mar 2016 10:39 pm, Chris Angelico wrote: > >> On Tue, Mar 1, 2016 at 10:18 PM, Steven D'Aprano >> wrote: >>> I cannot imagine why you would want to reload() in production code. That >>> would imply that your production code is modifying already-imported >>> modules, then wanting to import them again. Why would anyone in their >>> right mind do that? Possibly because they lost a bet? >> >> BartC and I have both done this exact thing, though neither of us used >> Python for it. It's not so insane as you might think; sometimes you >> want to update code, but you don't want to restart a process. Just >> because you've never had a process that hits a year of uptime doesn't >> mean nobody else does. :) > > I've had an uptime of over 400 days on my laptop, without logging out, so > yes I have had processes with over a year of uptime. But that doesn't mean > I expect to modify the code of a running process and reload it. Modify > data, sure, but not code. > > I'll grudgingly accept that, just maybe, if you have some sort of plugin > system, you may want to unload and reload plugins, replacing the old plugin > with a new one of the same name. Maybe you could get that to work in Python > using the import system. And then you expand the plugin system so it includes everything other than a kernel that manages plugins, and you now have the kind of system I'm talking about. My MUD server and client are both structured like this: 1) Startup file, which does basic setup, initializes all other files, then (in the server) binds to a port and starts listening. It then drops to a back-end loop, either threaded or async I/O. 2) Globally-available utilities toolbox. Can be reloaded to add or edit any of the functions/classes in it. 3) (Client only) GUI handling file. Creates the window, handles user interaction. Can be reloaded, although since it doesn't destroy and recreate the window, some changes won't take effect until full restart. 4) All other plugins, which can do pretty much anything. The system is quite happy to add, remove, and update these at any time. Everything except the startup file can be edited after startup. That's basically _every_ piece of code in the whole project. Major features can be implemented and debugged without resetting anything, and once I'm satisfied with the code, I commit and push, and end users can pull that change without restarting. If I were to implement this in Python, I would probably do it by reimplementing module-like behaviour, rather than actually using import. But it could be made to work, and it definitely has its benefits. On the server, it's crucial - I can't kick my users off every time I make changes; even on the client, having to restart and reconnect for every change would be tedious, although that's more for my own debugging than for other people's usage (it's not as big a deal to update the program once a week and then have to restart). ChrisA