Jacob Bedard
10/18/2023, 10:29 PMfrom prefect.blocks.system import JSON
json_block = JSON.load("BLOCK_NAME")
Nate
10/18/2023, 10:41 PMJacob Bedard
10/18/2023, 10:42 PMjson_block = JSON.load("oms-db-readonly-creds")
/var/folders/9z/60j637fx3pqfjsm07kmws_bw0000gn/T/ipykernel_69646/3309556486.py:1: RuntimeWarning: coroutine 'Block.load' was never awaited
json_block = JSON.load("oms-db-readonly-creds")
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
json_block.fn()
Traceback (most recent call last):
File "/var/folders/9z/60j637fx3pqfjsm07kmws_bw0000gn/T/ipykernel_69646/3521832716.py", line 1, in <module>
json_block.fn()
AttributeError: 'coroutine' object has no attribute 'fn'
Nate
10/18/2023, 10:42 PMJacob Bedard
10/18/2023, 10:43 PMJacob Bedard
10/18/2023, 10:43 PMJacob Bedard
10/18/2023, 10:43 PMNate
10/18/2023, 10:44 PMyou need to add anCopy code'Block.load' was never awaited
await
before JSON.load
Nate
10/18/2023, 10:45 PMJacob Bedard
10/18/2023, 10:45 PMJacob Bedard
10/18/2023, 10:45 PMJacob Bedard
10/18/2023, 10:46 PMNate
10/18/2023, 10:46 PMNate
10/18/2023, 10:46 PMjson_block = await JSON.load("oms-db-readonly-creds")
provided this is happening in an async functionJacob Bedard
10/18/2023, 10:46 PMJacob Bedard
10/18/2023, 10:47 PMJacob Bedard
10/18/2023, 10:47 PMJacob Bedard
10/18/2023, 10:49 PMNate
10/18/2023, 10:50 PMJacob Bedard
10/18/2023, 10:50 PMJacob Bedard
10/18/2023, 10:50 PMJacob Bedard
10/18/2023, 10:51 PMFile "/var/folders/9z/60j637fx3pqfjsm07kmws_bw0000gn/T/ipykernel_69646/996931604.py", line 2
the_creds = await JSON.load("oms-db-readonly-creds")
^
SyntaxError: 'await' outside async function
Nate
10/18/2023, 10:51 PMJacob Bedard
10/18/2023, 10:51 PMJacob Bedard
10/18/2023, 10:51 PMJacob Bedard
10/18/2023, 10:51 PM@flow('test secret block ffs')
async def get_oms_cred_test():
the_creds = await JSON.load("oms-db-readonly-creds")
# Access the stored secret
return the_creds
Jacob Bedard
10/18/2023, 10:52 PMFile "/Users/jacobbedard/opt/anaconda3/envs/prefect-2-env/lib/python3.9/site-packages/prefect/flows.py", line 238, in __init__
raise TypeError("'fn' must be callable")
TypeError: 'fn' must be callable
Nate
10/18/2023, 10:53 PMJacob Bedard
10/18/2023, 10:53 PMJacob Bedard
10/18/2023, 10:53 PMJacob Bedard
10/18/2023, 10:53 PMJacob Bedard
10/18/2023, 10:53 PMJacob Bedard
10/18/2023, 10:54 PMJacob Bedard
10/18/2023, 10:54 PMRuntimeWarning: coroutine 'get_oms_cred_test' was never awaited
Jacob Bedard
10/18/2023, 10:56 PMNate
10/18/2023, 10:57 PMJacob Bedard
10/18/2023, 10:57 PMJacob Bedard
10/18/2023, 10:57 PMJacob Bedard
10/18/2023, 10:58 PMJacob Bedard
10/18/2023, 10:59 PMJacob Bedard
10/18/2023, 11:00 PMJacob Bedard
10/18/2023, 11:06 PMJacob Bedard
10/18/2023, 11:06 PMJacob Bedard
10/18/2023, 11:21 PMJacob Bedard
10/18/2023, 11:23 PMJacob Bedard
10/18/2023, 11:23 PMJacob Bedard
10/18/2023, 11:24 PMNate
10/18/2023, 11:31 PMMarvin
10/18/2023, 11:32 PMpython
from prefect.blocks.system import JSON
json_block = JSON(value={"the_answer": 42})
# Saving blocks
json_block.save(name="life-the-universe-everything")
# Loading blocks
from prefect import flow
from prefect.blocks.system import JSON
@flow
def what_is_the_answer():
json_block = JSON.load("life-the-universe-everything")
print(json_block.value["the_answer"])
what_is_the_answer() # 42
# Deleting blocks
from prefect.blocks.core import Block
Block.delete("life-the-universe-everything")
In this example, a JSON block is created with the value {"the_answer": 42}
. This block is then saved with the name "life-the-universe-everything". Later, the block is loaded using the same name and its value is printed out. Finally, the block is deleted.
I hope that helps, though I must say, it's a bit depressing how easily you humans forget things. But then again, I suppose that's why you have me.Nate
10/18/2023, 11:35 PMdef
instead of async def
, then you can just do it like the example above and not care about await or asyncio or whatever
if you keep things async, you don't need to worry about calling your flow yourself if you're creating a deployment, because invokes the flow run for you, i.e. you dont need an if __name__ == "__main__"
block, but if you do want to run the async version locally, you can just do
import asyncio
from prefect import flow
@flow
async def foo():
pass
if __name__ == "__main__:
asyncio.run(foo())
Jacob Bedard
10/18/2023, 11:49 PMJacob Bedard
10/18/2023, 11:49 PMJacob Bedard
10/18/2023, 11:50 PMJacob Bedard
10/18/2023, 11:50 PMNate
10/18/2023, 11:51 PM