[Python-sdk] Passing an argument through the workflow constructor

Hello,
I’m new to temporal.
I have a workflow that require some global variable nlp_regression_param.

This is how we do it in JAVA:

worker.addWorkflowImplementationFactory(NlpAnalyzeWorkflow.class, (Functions.Func<NlpAnalyzeWorkflow>) () -> new NlpAnalyzeWorkflowImpls(supportedLanguages.getValue()));

This is our implementation in Python:

@workflow.defn
class NlpWorkflow:
    business_area = None

    def __init__(self, nlp_regression_param: NlpRegression) -> None:
        self.nlp_regression_param = nlp_regression_param

async def main():
    run_futures = []

    # Run the workers for the individual task queues
    handle = Worker(
        client,
        task_queue=queue_id,
        activities=[
            nlp_regression_activity.activity_1,
            nlp_regression_activity.run_activity_2,

        ],
        workflows=[NlpWorkflow],
    )

    run_futures.append(handle.run())



if __name__ == "__main__":
    loop = asyncio.new_event_loop()

    try:
        nlp_regression_activity = NlpRegressionActivity()
        loop.run_until_complete(main())
    except KeyboardInterrupt:
        interrupt_event.set()
        loop.run_until_complete(loop.shutdown_asyncgens())
        logger.info("Shutting down workers")

We are getting this error: __init__() missing 1 required positional argument: 'nlp_regression_param'

Also, we tried this: workflows=[NlpWorkflow(nlp_regression_param)] but we had this error: Workflow <unknown> missing attributes, was it decorated with @workflow.defn?

Workflows cannot have constructor parameters. Workflows should be isolated, they run in a sandbox so a global in the workflow file will be re-evaluated on each workflow run. If a workflow needs something it should be passed in via workflow input. If you must do this, you can put the global in another module and pass-through the import. But usually using globals in a workflow can lead to non-deterministic execution because the global does not have the same values on each worker.