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


Groups > comp.lang.python > #111715

Technique for safely reloading dynamically generated module

From Malcolm Greene <python@bdurham.com>
Newsgroups comp.lang.python
Subject Technique for safely reloading dynamically generated module
Date 2016-07-21 10:42 -0400
Message-ID <mailman.25.1469112144.22221.python-list@python.org> (permalink)
References <1469112141.379402.672830825.05659985@webmail.messagingengine.com>

Show all headers | View raw


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()
 
Thank you,
Malcolm
 

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


Thread

Technique for safely reloading dynamically generated module Malcolm Greene <python@bdurham.com> - 2016-07-21 10:42 -0400

csiph-web