def worker(self):
        """
        This is a sample generator which yields Deferreds when we are testing
        deferral and an ascending integer count otherwise.
        """
        i = 0
        while True:
            i += 1
            if self._doDeferNext:
                self._doDeferNext = False
                d = defer.Deferred()
                self.work.append(d)
                yield d
            elif self._doStopNext:
                return
            elif self._doDieNext:
                raise UnhandledException()
            else:
                self.work.append(i)
                yield i

        This is a sample generator which yields Deferreds when we are testing
        deferral and an ascending integer count otherwise.
        
stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_cooperator.py