简体   繁体   English

如何从命令行获取持久的python会话?

[英]How to get a persistent python session from command line?

I would like to get this behavior from the shell: 我想从shell中获取此行为:

$ python --new-session -c ''
$ python --use-session -c 'x = 42'
$ python --use-session -c 'print x'
42

Why? 为什么?

I am using a lot of helpers from the command line such as mako-render and jinja stuff to generate files for my (C/C++) project. 我使用命令行中的很多帮助程序,如mako-renderjinja stuff来为我的(C / C ++)项目生成文件。 At each invocation of Python, all the used modules are to be imported into the workspace which takes time. 在每次调用Python时,所有使用过的模块都要导入工作区,这需要时间。 By using a persistent workspace across my Python calls I can save a lot of processing time. 通过在Python调用中使用持久工作区,我可以节省大量处理时间。

My current solution is to use shelve to store everything I can between my sessions, but this is not convenient and I am looking for a less complicated solution. 我目前的解决方案是使用shelve来存储我的会话之间的所有内容,但这不方便,我正在寻找一个不那么复杂的解决方案。

I know something is possible using the Jupyter kernel. 我知道有可能使用Jupyter内核。 Unfortunately starting a new Python session connected to an existing kernel takes about 5 to 10 seconds on my system. 不幸的是,在我的系统上启动连接到现有内核的新Python会话大约需要5到10秒。

The following bash script is a draft approximation of what you need: 以下bash脚本是您需要的草稿近似值:

python-session: 蟒蛇会话:

#!/usr/bin/env bash

if [[ $# -ne 1 ]]
then
    echo 1>&2 "Usage: python-session <session-name>"
    exit 1
fi

sessionname="$1"
sessiondir=/tmp/python-session."$sessionname"
stdin="$sessiondir/stdin"
stdout="$sessiondir/stdout"

endsession()
{
    echo Exiting python session "$sessionname"
    rm -rf "$sessiondir"
}

newsession()
(
    echo "Starting new session $sessionname"
    mkdir "$sessiondir"
    trap "endsession" EXIT
    touch "$stdin"
    touch "$stdout"
    tail -f "$stdin"|python -i -c "import sys; sys.ps1=sys.ps2=''">"$stdout"
)

if [ ! -d "$sessiondir" ]
then
    newsession & disown
    while [ ! -e "$stdout" ]; do sleep 0.01; done
fi

echo "Connected to python session $1 (leave the session by typing CTRL-D)"
tail -f -n 0 "$stdout"&
tailpid=$!
trap "kill $tailpid" EXIT
cat >> "$stdin"

Demonstration: 示范:

$ ./python-session 1
Starting new session 1
Connected to python session 1 (leave the session by typing CTRL-D)
x=123
$ ./python-session 1
Connected to python session 1 (leave the session by typing CTRL-D)
print x
123
$ ./python-session 2
Starting new session 2
Connected to python session 2 (leave the session by typing CTRL-D)
print x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
x='abc'
$ ./python-session 1
Connected to python session 1 (leave the session by typing CTRL-D)
print x
123
$ ./python-session 2
Connected to python session 2 (leave the session by typing CTRL-D)
print x
abc
exit()

Exiting python session 2
$ ./python-session 1
Connected to python session 1 (leave the session by typing CTRL-D)
exit()

Exiting python session 1
$

Limitations of the draft implementation 实施草案的局限性

  • you must stop the session by entering the exit() command followed by an additional (even empty) line . 你必须通过输入exit()命令然后输入一个额外的(甚至是空的)行来停止会话。 After that you still have to hit CTRL-D to get disconnected from the now non-existent session 之后,您仍然必须按CTRL-D才能与现在不存在的会话断开连接

  • standard error stream of the python session is not captured 捕获python会话的标准错误流

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM