title: Global variable not defined with circular import and async/await
body: <p>In my project I have two modules: <code>main</code> and <code>api</code>. <code>main</code> imports <code>api</code> to perform certain API calls, and <code>api</code> imports <code>main</code> to use a logging function defined there. The code looks something like this</p>
  <p>Inside <code>main</code> module:</p>
  <pre><code>import asyncio
  import multiprocessing
  import api
  
  def log(msg):
      print(multiprocessing.current_process().pid). # prints out a different pid than the main process, I don't understand why
      with lock: # raises NameError: name 'lock' is not defined
          some_log_func(msg)
  
  async def main():
      global lock
  
      lock = multiprocessing.Lock()
      
      data = await api.query()
  
      # ... some multiprocessing stuff
  
  
  if __name__ == '__main__':
      loop = asyncio.get_event_loop()
      try:
          loop.run_until_complete(main())
      finally:
          loop.close()
  </code></pre>
  <p>Inside <code>api</code> module:</p>
  <pre><code>import main
  
  async def query():
      main.log('Querying...') # causes the NameError shown above
      # do some stuff
  </code></pre>
  <p>As mentioned, running the <code>main</code> module raises <code>NameError: name 'lock' is not defined</code>. I'm using a <code>multiprocessing.Pool</code> later in the <code>main</code> module but at the point when this error occurs, it's running in the main process and the pool hasn't been initialized yet.</p>
  <p>Where am I going wrong here?</p>
  <p>It's also worth noting that my <code>log</code> function is used in the worker processes as well, so I've been printing out the pid of the current process for debugging, and though I haven't spawned any processes yet, when the <code>log</code> function gets called it prints out a pid which doesn't belong to the main process (I'm guessing async/await uses separate processes internally?). If it does use separate processes, then that would make sense since the global variable won't be defined in that child process, but if so, is there a way for the child process to inherit the lock?</p>