Esempio n. 1
0
        def __init__(self, true_self):
            self.__dict__ = true_self.__dict__
            stack = [s.strip() for s in traceback.format_stack()]
            # Remove top three stack entries from adding the wrapper
            self.creation_stack = '\n'.join(stack[:-3])
            self._tf_ref_id = next(_REF_ITER)
            _REF_INFO[self._tf_ref_id] = _RefInfoField(type_=type(x),
                                                       repr_=repr(x),
                                                       creation_stack=stack,
                                                       object_used=False)

            # Create a finalizer for self, which will be called when self is
            # garbage collected.  Can't add self as the args because the
            # loop will break garbage collection.  We keep track of
            # ourselves via python ids.
            weakref.finalize(self, _deleted, self._tf_ref_id, fatal_error)
Esempio n. 2
0
    def __init__(self, true_self):
      self.__dict__ = true_self.__dict__
      stack = [s.strip() for s in traceback.format_stack()]
      # Remove top three stack entries from adding the wrapper
      self.creation_stack = '\n'.join(stack[:-3])
      self._tf_ref_id = next(_REF_ITER)
      _REF_INFO[self._tf_ref_id] = _RefInfoField(
          type_=type(x),
          repr_=repr(x),
          creation_stack=stack,
          object_used=False)

      # Create a finalizer for self, which will be called when self is
      # garbage collected.  Can't add self as the args because the
      # loop will break garbage collection.  We keep track of
      # ourselves via python ids.
      weakref.finalize(self, _deleted, self._tf_ref_id, fatal_error)
    def run_in_child(cls):  # pragma: no cover (executed in subprocess)
        def error():
            # Create an atexit finalizer from inside a finalizer called
            # at exit.  This should be the next to be run.
            g1 = weakref.finalize(cls, print, 'g1')
            print('f3 error')
            1/0

        # cls should stay alive till atexit callbacks run
        f1 = weakref.finalize(cls, print, 'f1', _global_var)
        f2 = weakref.finalize(cls, print, 'f2', _global_var)
        f3 = weakref.finalize(cls, error)
        f4 = weakref.finalize(cls, print, 'f4', _global_var)

        assert f1.atexit == True
        f2.atexit = False
        assert f3.atexit == True
        assert f4.atexit == True
    def test_finalize(self):
        def add(x,y,z):
            res.append(x + y + z)
            return x + y + z

        a = self.A()

        res = []
        f = weakref.finalize(a, add, 67, 43, z=89)
        self.assertEqual(f.alive, True)
        self.assertEqual(f.peek(), (a, add, (67,43), {'z':89}))
        self.assertEqual(f(), 199)
        self.assertEqual(f(), None)
        self.assertEqual(f(), None)
        self.assertEqual(f.peek(), None)
        self.assertEqual(f.detach(), None)
        self.assertEqual(f.alive, False)
        self.assertEqual(res, [199])

        res = []
        f = weakref.finalize(a, add, 67, 43, 89)
        self.assertEqual(f.peek(), (a, add, (67,43,89), {}))
        self.assertEqual(f.detach(), (a, add, (67,43,89), {}))
        self.assertEqual(f(), None)
        self.assertEqual(f(), None)
        self.assertEqual(f.peek(), None)
        self.assertEqual(f.detach(), None)
        self.assertEqual(f.alive, False)
        self.assertEqual(res, [])

        res = []
        f = weakref.finalize(a, add, x=67, y=43, z=89)
        del a
        self._collect_if_necessary()
        self.assertEqual(f(), None)
        self.assertEqual(f(), None)
        self.assertEqual(f.peek(), None)
        self.assertEqual(f.detach(), None)
        self.assertEqual(f.alive, False)
        self.assertEqual(res, [199])
    def test_order(self):
        a = self.A()
        res = []

        f1 = weakref.finalize(a, res.append, 'f1')
        f2 = weakref.finalize(a, res.append, 'f2')
        f3 = weakref.finalize(a, res.append, 'f3')
        f4 = weakref.finalize(a, res.append, 'f4')
        f5 = weakref.finalize(a, res.append, 'f5')

        # make sure finalizers can keep themselves alive
        del f1, f4

        self.assertTrue(f2.alive)
        self.assertTrue(f3.alive)
        self.assertTrue(f5.alive)

        self.assertTrue(f5.detach())
        self.assertFalse(f5.alive)

        f5()                       # nothing because previously unregistered
        res.append('A')
        f3()                       # => res.append('f3')
        self.assertFalse(f3.alive)
        res.append('B')
        f3()                       # nothing because previously called
        res.append('C')
        del a
        self._collect_if_necessary()
                                   # => res.append('f4')
                                   # => res.append('f2')
                                   # => res.append('f1')
        self.assertFalse(f2.alive)
        res.append('D')
        f2()                       # nothing because previously called by gc

        expected = ['A', 'f3', 'B', 'C', 'f4', 'f2', 'f1', 'D']
        self.assertEqual(res, expected)
Esempio n. 6
0
    def _start_cluster(self, spec, skein_client=None):
        """Start the cluster and initialize state"""
        if skein_client is None:
            skein_client = skein.Client()

        app = skein_client.submit_and_connect(spec)
        try:
            scheduler_address = app.kv.wait('dask.scheduler').decode()
        except BaseException:
            # Failed to connect, kill the application and reraise
            skein_client.kill_application(app.id)
            raise

        # Ensure application gets cleaned up
        self._finalizer = weakref.finalize(self, app.shutdown)

        self.app_id = app.id
        self.application_client = app
        self.scheduler_address = scheduler_address
Esempio n. 7
0
 def _add_finalizer(self, func, *args, **kwargs):
     if self not in _finalizers:
         _finalizers[self] = []
     _finalizers[self].append(finalize(self, func, *args, **kwargs))
Esempio n. 8
0
 def __init__(self, suffix=None, prefix=None, dir=None):
     self.name = mkdtemp(suffix, prefix, dir)
     self._finalizer = finalize(
         self, self._cleanup, self.name,
         warn_message="Implicitly cleaning up {!r}".format(self))
 def error():
     # Create an atexit finalizer from inside a finalizer called
     # at exit.  This should be the next to be run.
     g1 = weakref.finalize(cls, print, 'g1')
     print('f3 error')
     1/0