Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #111718
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Peter Otten <__peter__@web.de> |
| Newsgroups | comp.lang.python |
| Subject | Re: Technique for safely reloading dynamically generated module |
| Date | Thu, 21 Jul 2016 17:40:16 +0200 |
| Organization | None |
| Lines | 58 |
| Message-ID | <mailman.28.1469115627.22221.python-list@python.org> (permalink) |
| References | <1469112141.379402.672830825.05659985@webmail.messagingengine.com> <nmqqd2$lcd$1@ger.gmane.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Trace | news.uni-berlin.de C0OonC52PGQCa5QYTT3iuwOpyTFW/stQQ7OcB/3VuegA== |
| Return-Path | <python-python-list@m.gmane.org> |
| 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; '"""': 0.05; 'here?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:module': 0.09; 'python': 0.10; 'def': 0.13; 'file,': 0.15; 'compile,': 0.16; 'language)': 0.16; 'malcolm': 0.16; 'missing,': 0.16; 'placeholder': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'reload': 0.16; 'sys.path': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'runs': 0.18; 'visible': 0.22; 'code.': 0.23; 'import': 0.24; 'module': 0.25; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:" 20': 0.26; 'skip:e 30': 0.27; 'function': 0.28; 'actual': 0.28; "we're": 0.30; 'code': 0.30; 'run': 0.33; 'source': 0.33; 'file': 0.34; 'server': 0.34; 'step': 0.36; 'should': 0.36; 'there': 0.36; 'created': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:org': 0.37; 'suggestion': 0.37; 'associated': 0.38; 'version': 0.38; 'application': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'your': 0.60; 'avoid': 0.61; 'our': 0.64; 'techniques': 0.65; 'dsl': 0.84; 'of?': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | p57bd89c6.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.22 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <nmqqd2$lcd$1@ger.gmane.org> |
| X-Mailman-Original-References | <1469112141.379402.672830825.05659985@webmail.messagingengine.com> |
| Xref | csiph.com comp.lang.python:111718 |
Show key headers only | View raw
Malcolm Greene wrote:
> We're designing a server application that parses a custom DSL (domain
> specific language) source file, generates a Python module with the
> associated logic, and runs the associated code. Since this is a server
> application, we need to reload the module after each regeneration. Is
> this process is simple as the following pseudo code or are there other
> issues we need to be aware of? Are there better techniques for this
> workflow (eval, compile, etc)?
>
> We're working in Python 3.5.1.
>
> import importlib
>
> # custom_code is the module our code will generate - a version of this
> # file will always be present
> # if custom_code.py is missing, a blank version of this file is created
> # before this step
> import custom_code
>
> while True:
> # (re)generates custom_code.py visible in sys.path
> generate_custom_code( source_file )
>
> # reload the module whose source we just generated
> importlib.reload( custom_code )
>
> # run the main code in generated module
> custom_code.go()
If the go() function in that loop is the only place where the generated
module is used you can avoid writing code to the file system altogether.
Here's a really simple alternative suggestion based on exec():
SOURCEFILE = "whatever.dsl"
def make_func(sourcefile):
ns = {}
exec(generated_code(sourcefile), ns)
return ns["go"]
def generated_code(sourcefile):
# placeholder for your actual source generation
return """
def go():
print("Generated from", {!r})
""".format(sourcefile)
while True:
# XXX should we wait for updates of sourcefile here?
go = make_func(SOURCEFILE)
go()
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Technique for safely reloading dynamically generated module Peter Otten <__peter__@web.de> - 2016-07-21 17:40 +0200
csiph-web