我正在阅读Dagster教程,认为这是一个连接到本地mongodb的很好的练习。
from dagster import get_dagster_logger, job, op
from pymongo import MongoClient
@op
def connection():
client = MongoClient("mongodb://localhost:27017/")
return client["development"]
@job
def execute():
client = connection()
get_dagster_logger().info(f"Connection: {client} ")达格斯特错误:
dagster.core.errors.DagsterExecutionHandleOutputError: Error occurred while handling output "result" of step "connection":
File "/usr/local/lib/python3.9/site-packages/dagster/core/execution/plan/execute_plan.py", line 232, in dagster_event_sequence_for_step
for step_event in check.generator(step_events):
File "/usr/local/lib/python3.9/site-packages/dagster/core/execution/plan/execute_step.py", line 348, in core_dagster_event_sequence_for_step
for evt in _type_check_and_store_output(step_context, user_event, input_lineage):
File "/usr/local/lib/python3.9/site-packages/dagster/core/execution/plan/execute_step.py", line 405, in _type_check_and_store_output
for evt in _store_output(step_context, step_output_handle, output, input_lineage):
File "/usr/local/lib/python3.9/site-packages/dagster/core/execution/plan/execute_step.py", line 534, in _store_output
for elt in iterate_with_context(
File "/usr/local/lib/python3.9/site-packages/dagster/utils/__init__.py", line 400, in iterate_with_context
return
File "/usr/local/Cellar/python@3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/contextlib.py", line 137, in __exit__
self.gen.throw(typ, value, traceback)
File "/usr/local/lib/python3.9/site-packages/dagster/core/execution/plan/utils.py", line 73, in solid_execution_error_boundary
raise error_cls(
The above exception was caused by the following exception:
TypeError: cannot pickle '_thread.lock' object
File "/usr/local/lib/python3.9/site-packages/dagster/core/execution/plan/utils.py", line 47, in solid_execution_error_boundary
yield
File "/usr/local/lib/python3.9/site-packages/dagster/utils/__init__.py", line 398, in iterate_with_context
next_output = next(iterator)
File "/usr/local/lib/python3.9/site-packages/dagster/core/execution/plan/execute_step.py", line 524, in _gen_fn
gen_output = output_manager.handle_output(output_context, output.value)
File "/usr/local/lib/python3.9/site-packages/dagster/core/storage/fs_io_manager.py", line 124, in handle_output
pickle.dump(obj, write_obj, PICKLE_PROTOCOL)我已经在一个ipython中进行了本地测试,它可以工作,所以这个问题与dagster有关。
发布于 2022-05-04 19:10:02
默认的IOManager要求操作的输入和输出是可选的--很可能您的MongoClient不是。您可能需要尝试使用Dagster的@资源方法对其进行重构。这允许您将资源从外部定义到@op,并使以后在测试中模拟这些资源变得非常容易。您的代码应该如下所示:
from dagster import get_dagster_logger, job, op, resource
from pymongo import MongoClient
@resource
def mongo_client():
client = MongoClient("mongodb://localhost:27017/")
return client["development"]
@op(
required_resource_keys={'mongo_client'}
)
def test_client(context):
client = context.resources.mongo_client
get_dagster_logger().info(f"Connection: {client} ")
@job(
resource_defs={'mongo_client': mongo_client}
)
def execute():
test_client()还请注意,我将测试代码移到另一个@op中,然后只从execute @job中调用该op。这是因为作业定义中的代码是在加载时编译的,并且只用于描述要执行的操作图。执行任务的所有通用编程都需要包含在@op代码中。
@资源模式的真正巧妙之处在于,这使得使用模拟资源或更一般的交换资源进行测试变得非常容易。假设您想要一个模拟的客户端,这样您就可以运行作业代码,而不需要实际访问数据库。您可以这样做:
@resource
def mocked_mongo_client():
from unittest.mock import MagicMock
return MagicMock()
@graph
def execute_graph():
test_client()
execute_live = execute_graph.to_job(name='execute_live',
resource_defs={'mongo_client': mongo_client,})
execute_mocked = execute_graph.to_job(name='execute_mocked',
resource_defs={'mongo_client': mocked_mongo_client,})这使用Dagster的@图模式来描述ops的DAG,然后使用GraphDefinition对象上的.to_job()方法以不同的方式配置图形。这样,您可以具有相同的底层op结构,但传递不同的资源、标记、执行器等。
https://stackoverflow.com/questions/72118156
复制相似问题