Can I store global variables in the prefect context so that I can reuse them in other tasks?

Technically, it is possible and this documentation page shows how you could do that.

However, the Prefect context is not meant to be mutable so adding an item to it inside a task will not take effect in downstream tasks.

Recommended approach

The following Prefect features allow you to set and dynamically overwrite key-value pairs in your flow:

  • Parameter values can be dynamically overwritten for a specific flow run, allowing you to inject custom key-value pairs into your flow.
  • The KV Store allows you to set and retrieve custom key-value pairs within your tasks.