I'm having trouble understanding how Contexts work...
# ask-community
t
I'm having trouble understanding how Contexts work. This code always seems to return None
Copy code
prefect.context.get('flow_run_name')
How do I get the flow run name (like zealous-dormouse) from inside the flow?
d
Hi @Trevor Kramer! I believe that will only be populated if the Flow is run using an orchestration backend (Prefect Cloud/Server) and not if you’re calling
flow.run()
Let me make sure that’s in context 👍
t
I am using the Prefect Cloud orchestration backend
in hybrid mode
Looks like that should be available to you
Can you show me that line of code in its larger context? Are you running it inside of a task?
t
Copy code
batch_task = BatchSubmit(job_name=prefect.context.get('flow_run_name', 'RF Training Job'))(job_queue=export_result['job_queue_arn'],
                                                                        job_definition=export_result[
                                                                            'job_definition_arn'],
                                                                        batch_kwargs={
                                                                            'parameters': {
                                                                                'x_train': x_train_location,
                                                                                'y_train': y_train_location,
                                                                                'model_location': model_location,
                                                                                'max_features': max_features,
                                                                                'min_samples_leaf': min_samples_leaf,
                                                                                'min_samples_split': min_samples_split,
                                                                                'n_estimators': n_estimators,
                                                                                'n_streams': n_streams,
                                                                                'random_state': random_state}})
AWSClientWait(client='batch', waiter_name='JobComplete')(waiter_kwargs={'jobs': [batch_task]})
this in run inside a with Flow() block
d
🧐
This won’t work unfortunately,
prefect.context.get()
needs to be called from within a Task’s
run
method, not just from inside the Flow’s context block
We could make a change to the
BatchSubmit
task so that the job name can be parameterized from context
@Marvin open “Parameterize ‘job_name’ for BatchSubmit from Context”
t
So I should be able to wrap the BatchSubmit call in a task and be able to access the context there? When I try that I get Could not infer an active Flow context.
d
I think you’ll need to copy the code from the BatchSubmit task into a custom task in order to achieve your goal until we make the improvement to the Task in the library