推荐学习书目
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 · Oct 24, 2019 · 4603 views
    This topic created in 2411 days ago, the information mentioned may be changed or developed.
    最近想自己写一个 python 的并行框架,在服务器端根据需要实时生成 python 脚本并分发到个节点上运行。问一下如何把指定的几个函数写进脚本文件呢?
    4 replies    2019-10-28 09:22:24 +08:00
    ClericPy
        1
    ClericPy  
       Oct 24, 2019
    大部分情况都是把参数打包成 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  
       Oct 24, 2019   ❤️ 1
    写模板,然后动态渲染保存为 python 文件,以前做过类似的项目,只是没有分发到其它服务器上执行这一步
    cominghome
        3
    cominghome  
       Oct 25, 2019
    为啥不把脚本转成动态读参的
    acone2003
        4
    acone2003  
    OP
       Oct 28, 2019
    谢谢楼上各位,已解决,用 inspect.getsourcelines()函数。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4760 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 01:07 · PVG 09:07 · LAX 18:07 · JFK 21:07
    ♥ Do have faith in what you're doing.