title: Luigi global variables
body: <p>I would like to set some target paths as global variables in Luigi.</p>
  
  <p>The reason is that the target paths I'm using are based on the last run of a given numerical weather prediction (NWP), and it takes some time to get the value. Once I have checked which is the last run I create a path in which I will put several target files (with the same parent folder). </p>
  
  <p>I'm currently repeating a similar call to get the value of the parent path for several tasks, and it would be much efficient to set this path as a global variable. I have tried to define global variable from within one function (get_target_path) called by a luigi class, but it looks like the global variable doesn't persist when I  get back to Luigi pipeline.</p>
  
  <p>This is moreover how my code looks like:</p>
  
  <pre><code>class GetNWP(luigi.Task):
      """
      Download the NWP data.
      """
      product_id = luigi.Parameter()
      date = luigi.Parameter(default=datetime.today().strftime('%Y%m%d'))
      run_hr = luigi.Parameter(default='latest')
  
      def requires(self):
          return None
      def output(self):
          path = get_target_path(self.product_id, self.date, self.run_hr,
                                 type='getNWP')
          return luigi.LocalTarget(path)
      def run(self):
          download_nwp_data(self.product_id, self.date, self.run_hr)
  
  
  class GetNWP_GFS(luigi.Task):
      """
      GFS data.
      """
      product_id = luigi.Parameter()
      date = luigi.Parameter(default=datetime.today().strftime('%Y%m%d'))
      run_hr = luigi.Parameter(default='latest')
  
      def requires(self):
          return None
      def output(self):
          path = get_target_path(self.product_id_PV, self.date, self.run_hr,
                                 type='getNWP_GFS')
          return luigi.LocalTarget(path)
      def run(self):
          download_nwp_data(self.product_id, self.date, self.run_hr,
                            type='getNWP_GFS')
  
  
  class Predict(luigi.Task):
      """
      Create forecast.
      """
      product_id = luigi.Parameter(default=None)
      date = luigi.Parameter(default=datetime.today().strftime('%Y%m%d'))
      run_hr = luigi.Parameter(default='latest')
      horizon = luigi.Parameter(default='DA')
  
      def requires(self):
          return [
                  GetNWP_GFS(self.product_id, self.date, self.run_hr),
                  GetNWP(self.product_id, self.date, self.run_hr)
                  ]
      def output(self):
          path = get_target_path(self.product_id, self.date, self.run_hr,
                                 type='predict', horizon=self.horizon)
          return luigi.LocalTarget(path)
      def run(self):
          get_forecast(self.product_id, self.date, self.run_hr)
  </code></pre>
  
  <p>The function get_target_path defines a target path based on the input parameters. I would like this function to set global variables that would be accessible from Luigi. For example as follows (just the code for the getNWP task):</p>
  
  <pre><code>def get_target_path(product_id, date, run_hr, type=None, horizon='DA'):
          """
          Obtain target path.
          """
          if type == 'getNWP_GFS':
              if 'path_nwp_gfs' in globals():
                  return path_nwp_gfs
              else:
                  ...
          elif type == 'getNWP':
              if 'path_nwp_model' in globals():
                  return path_nwp_model
              else:
                  filename = f'{nwp_model}_{date}_{run_hr}_{horizon}.{ext}'
                  path = Path(db_dflt['app_data']['nwp_folder'])
                  create_directory(path)
                  global path_nwp_model
                  path_nwp_model = Path(path) / filename
          elif type == 'predict':
              if 'path_predict' in globals():
                  return path_predict
              else:
                  ...
  </code></pre>
  
  <p>The global variable defined in this function doesn't exist when I'm back to Luigi.</p>
  
  <p>Any ideas on how to solve this problem will be appreciated!</p>