Fabric - Python 3 - 什么是上下文,它必须包含什么,为什么我需要传递它?
Fabric - Python 3 - What is context and what does it have to contain and why do I need to pass it?
这是我的面料代码:
from fabric import Connection, task
server = Connection(host="usrename@server.com:22", connect_kwargs={"password": "mypassword"})
@task
def dostuff(somethingmustbehere):
server.run("uname -a")
这段代码工作得很好。当我执行 fab dostuff
时,它会执行我希望它执行的操作。
当我删除 somethingmustbehere
时,我收到此错误消息:
raise TypeError("Tasks must have an initial Context argument!")
TypeError: Tasks must have an initial Context argument!
我从未在我的代码中的任何地方定义 somethingmustbehere
。我只是把它放进去,错误消失了,一切正常。但为什么?这个变量是什么?为什么我需要它?为什么如此重要?如果它如此重要,为什么它只能是空的?我真的迷路了。是的,它可以工作,但我不能 运行 我不理解的代码。这让我发疯。 :-)
请注意,我说的是 Python 3(!) 版本的 Fabric!
Fabric 版本为 2.4.0
为了能够 运行 一个 @task
你需要一个上下文参数。 Fabric 使用 invoke task()
期望看到上下文对象。通常我们将变量命名为 c
或 ctx
(我总是用它来使它更清楚)。我不喜欢使用 c
,因为我通常将其用于连接
检查这个 line on github from invoke package repo,你会看到当上下文参数不存在时它会引发异常,但它没有解释原因!
要了解有关 Context 对象的更多信息,它是什么以及我们为什么需要它,您可以在 pyinvoke 站点上阅读以下内容:
Aside: what exactly is this ‘context’ arg anyway? A common problem
task runners face is transmission of “global” data - values loaded
from configuration files or other configuration vectors, given via CLI
flags, generated in ‘setup’ tasks, etc.
Some libraries (such as Fabric 1.x) implement this via module-level
attributes, which makes testing difficult and error prone, limits
concurrency, and increases implementation complexity.
Invoke encapsulates state in explicit Context objects, handed to tasks
when they execute . The context is the primary API endpoint, offering
methods which honor the current state (such as Context.run) as well as
access to that state itself.
检查这两个链接:
老实说,我浪费了很多时间来弄清楚上下文是什么以及为什么我的代码没有它就不会 运行。但在某些时候我只是放弃并开始使用使我的代码 运行 没有错误。
这是我的面料代码:
from fabric import Connection, task
server = Connection(host="usrename@server.com:22", connect_kwargs={"password": "mypassword"})
@task
def dostuff(somethingmustbehere):
server.run("uname -a")
这段代码工作得很好。当我执行 fab dostuff
时,它会执行我希望它执行的操作。
当我删除 somethingmustbehere
时,我收到此错误消息:
raise TypeError("Tasks must have an initial Context argument!")
TypeError: Tasks must have an initial Context argument!
我从未在我的代码中的任何地方定义 somethingmustbehere
。我只是把它放进去,错误消失了,一切正常。但为什么?这个变量是什么?为什么我需要它?为什么如此重要?如果它如此重要,为什么它只能是空的?我真的迷路了。是的,它可以工作,但我不能 运行 我不理解的代码。这让我发疯。 :-)
请注意,我说的是 Python 3(!) 版本的 Fabric! Fabric 版本为 2.4.0
为了能够 运行 一个 @task
你需要一个上下文参数。 Fabric 使用 invoke task()
期望看到上下文对象。通常我们将变量命名为 c
或 ctx
(我总是用它来使它更清楚)。我不喜欢使用 c
,因为我通常将其用于连接
检查这个 line on github from invoke package repo,你会看到当上下文参数不存在时它会引发异常,但它没有解释原因!
要了解有关 Context 对象的更多信息,它是什么以及我们为什么需要它,您可以在 pyinvoke 站点上阅读以下内容:
Aside: what exactly is this ‘context’ arg anyway? A common problem task runners face is transmission of “global” data - values loaded from configuration files or other configuration vectors, given via CLI flags, generated in ‘setup’ tasks, etc.
Some libraries (such as Fabric 1.x) implement this via module-level attributes, which makes testing difficult and error prone, limits concurrency, and increases implementation complexity.
Invoke encapsulates state in explicit Context objects, handed to tasks when they execute . The context is the primary API endpoint, offering methods which honor the current state (such as Context.run) as well as access to that state itself.
检查这两个链接:
老实说,我浪费了很多时间来弄清楚上下文是什么以及为什么我的代码没有它就不会 运行。但在某些时候我只是放弃并开始使用使我的代码 运行 没有错误。