有没有办法只在分配的值不是 None 时才进行分配,否则什么都不做?
我们当然可以这样做:
x = get_value() if get_value() is not None
Run Code Online (Sandbox Code Playgroud)
但这将读取该值两次。我们可以将它缓存到一个局部变量中:
v = get_value()
x = v if v is not None
Run Code Online (Sandbox Code Playgroud)
但现在我们为一件简单的事情做了两个陈述。
我们可以写一个函数:
def return_if_not_none(v, default):
if v is not None:
return v
else:
return default
Run Code Online (Sandbox Code Playgroud)
然后做x = return_if_not_none(get_value(), x)。但是肯定已经有一个 Python 习惯用法可以实现这一点,无需访问x或get_value()两次,也无需创建变量?
换句话说,假设=??是一个类似于C# null 合并运算符的 Python 运算符。与 C# 不同??=,我们虚构的运算符检查右侧是否为None:
x = 1
y = 2
z = None
x =?? y …Run Code Online (Sandbox Code Playgroud) 我正在使用一个 Docker 映像,该映像以交互模式启动,如下所示:docker run -it --rm ubuntu bash
我使用的实际图像有许多复杂的参数,这就是为什么我编写了一个脚本来构建完整的docker run命令并为我启动它。随着逻辑变得越来越复杂,我想将脚本从 bash 迁移到 Python。
使用docker-py,我准备好了运行图像的一切。不过,似乎不支持docker.containers.run用于交互式 shell 。使用相反似乎合乎逻辑,所以我尝试了以下方法:subprocess
import subprocess
subprocess.Popen(['docker', 'run', '-it', '--rm', 'ubuntu', 'bash'])
Run Code Online (Sandbox Code Playgroud)
但这给了我:
$ python3 docker_run_test.py
$ unable to setup input stream: unable to set IO streams as raw terminal: input/output error
$
Run Code Online (Sandbox Code Playgroud)
请注意,错误消息出现在与 python 命令不同的 shell 提示符中。
我怎样才能python3 docker_run_test.py做相当于跑步的事情docker run -it --rm ubuntu bash?
我有 AirflowCeleryExecutor和 2 个工作人员一起运行。当我的 DAG 运行时,任务会在运行它们的工作线程的文件系统上生成日志。但是当我转到 Web UI 并单击任务日志时,我得到:
*** Log file does not exist: /usr/local/airflow/logs/test_dag/task2/2019-11-01T18:12:16.309655+00:00/1.log
*** Fetching from: http://70953abf1c10:8793/log/test_dag/task2/2019-11-01T18:12:16.309655+00:00/1.log
*** Failed to fetch log file from worker. HTTPConnectionPool(host='70953abf1c10', port=8793): Max retries exceeded with url: /log/test_dag/task2/2019-11-01T18:12:16.309655+00:00/1.log (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f329c3a2650>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))
Run Code Online (Sandbox Code Playgroud)
http://70953abf1c10:8793/显然不是worker的正确IP。然而,celery@70953abf1c10 这是芹菜中这名工人的名字。看起来 Airflow 正在尝试从 Celery 学习工作人员的 URL,但 Celery 却给出了工作人员的名字。我该如何解决这个问题?