V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
acone2003
V2EX  ›  Python

如何动态生成 Python 脚本文件?

  •  
  •   acone2003 · 2019-10-24 19:25:07 +08:00 · 3500 次点击
    这是一个创建于 1636 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近想自己写一个 python 的并行框架,在服务器端根据需要实时生成 python 脚本并分发到个节点上运行。问一下如何把指定的几个函数写进脚本文件呢?
    4 条回复    2019-10-28 09:22:24 +08:00
    ClericPy
        1
    ClericPy  
       2019-10-24 19:28:43 +08:00
    大部分情况都是把参数打包成 json, 然后另一边从消息队列读 json 按照参数执行函数吧...
    不过你这种要求的话
    你知道 linux 的管道符吧, 可以通过 curl 获取脚本文件然后用管道符直接执行, 这样甚至不用存储脚本文件, 文件在内存里就可以了

    通过 curl 加管道符使用在线代码
    curl http://www.xxx.com/xx.py | python - -xx xx >> /tmp/plan_b.log 2>&1
    不过实际上学会 xargs -i xxxx {} 更简单...

    这两种管道符的方式是我当年用的
    midtin
        2
    midtin  
       2019-10-24 21:08:01 +08:00   ❤️ 1
    写模板,然后动态渲染保存为 python 文件,以前做过类似的项目,只是没有分发到其它服务器上执行这一步
    cominghome
        3
    cominghome  
       2019-10-25 12:23:44 +08:00
    为啥不把脚本转成动态读参的
    acone2003
        4
    acone2003  
    OP
       2019-10-28 09:22:24 +08:00
    谢谢楼上各位,已解决,用 inspect.getsourcelines()函数。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2681 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:50 · PVG 23:50 · LAX 08:50 · JFK 11:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.