code: def pqueue(fn, queue):
      def sample_escape(tr, site):
          return (
              (site["name"] not in tr)
              and (site["type"] == "sample")
              and (not site["is_observed"])
          )
  
      def _fn(*args, **kwargs):
          for i in range(int(1e6)):
              assert (
                  not queue.empty()
              ), "trying to get() from an empty queue will deadlock"
  
              priority, next_trace = queue.get()
              try:
                  ftr = poutine.trace(
                      poutine.escape(
                          poutine.replay(fn, next_trace),
                          functools.partial(sample_escape, next_trace),
                      )
                  )
                  return ftr(*args, **kwargs)
              except NonlocalExit as site_container:
                  site_container.reset_stack()
                  for tr in poutine.util.enum_extend(
                      ftr.trace.copy(), site_container.site
                  ):
                      # add a little bit of noise to the priority to break ties...
                      queue.put(
                          (
                              tr.log_prob_sum().item()
                              - torch.rand(1).item() * 1e-2,
                              tr,
                          )
                      )
  
          raise ValueError("max tries ({}) exceeded".format(str(1e6)))
  
      return _fn
  
imports: import collections
  import functools
  import queue
  import torch
  import pyro.distributions
  import pyro.poutine
  from pyro.infer.abstract_infer import TracePosterior
  from pyro.poutine.runtime import NonlocalExit
label_desc: Use image node_id 2 for calling a global function with example usage: pqueue(fn, queue) and returns: _fn, unknown, ftr