Example #1
0
    def run_forever(self):
        while True:
            if self.q:
                t, cnt, cb, args = heapq.heappop(self.q)
                if __debug__:
                    log.debug("Next coroutine to run: %s", (t, cnt, cb, args))
#                __main__.mem_info()
                tnow = self.time()
                delay = t - tnow
                if delay > 0:
                    self.wait(delay)
            else:
                self.wait(-1)
                # Assuming IO completion scheduled some tasks
                continue
            if callable(cb):
                cb(*args)
            else:
                delay = 0
                try:
                    if __debug__:
                        log.debug("Coroutine %s send args: %s", cb, args)
                    if args == ():
                        ret = next(cb)
                    else:
                        ret = cb.send(*args)
                    if __debug__:
                        log.debug("Coroutine %s yield result: %s", cb, ret)
                    if isinstance(ret, SysCall1):
                        arg = ret.arg
                        if isinstance(ret, Sleep):
                            delay = arg
                        elif isinstance(ret, IORead):
#                            self.add_reader(ret.obj.fileno(), lambda self, c, f: self.call_soon(c, f), self, cb, ret.obj)
#                            self.add_reader(ret.obj.fileno(), lambda c, f: self.call_soon(c, f), cb, ret.obj)
#                            self.add_reader(arg.fileno(), lambda cb: self.call_soon(cb), cb)
                            self.add_reader(arg.fileno(), cb)
                            continue
                        elif isinstance(ret, IOWrite):
#                            self.add_writer(arg.fileno(), lambda cb: self.call_soon(cb), cb)
                            self.add_writer(arg.fileno(), cb)
                            continue
                        elif isinstance(ret, IOReadDone):
                            self.remove_reader(arg.fileno())
                        elif isinstance(ret, IOWriteDone):
                            self.remove_writer(arg.fileno())
                        elif isinstance(ret, StopLoop):
                            return arg
                    elif isinstance(ret, type_gen):
                        self.call_soon(ret)
                    elif ret is None:
                        # Just reschedule
                        pass
                    else:
                        assert False, "Unsupported coroutine yield value: %r (of type %r)" % (ret, type(ret))
                except StopIteration as e:
                    if __debug__:
                        log.debug("Coroutine finished: %s", cb)
                    continue
                self.call_later(delay, cb, *args)
Example #2
0
def pop_and_print(h):
    l = []
    while h:
        l.append(str(heapq.heappop(h)))
    print(' '.join(l))
Example #3
0
try:
    import uheapq as heapq
except:
    try:
        import heapq
    except ImportError:
        print("SKIP")
        raise SystemExit

try:
    heapq.heappop([])
except IndexError:
    print("IndexError")

try:
    heapq.heappush((), 1)
except TypeError:
    print("TypeError")

def pop_and_print(h):
    l = []
    while h:
        l.append(str(heapq.heappop(h)))
    print(' '.join(l))

h = []
heapq.heappush(h, 3)
heapq.heappush(h, 1)
heapq.heappush(h, 2)
print(h)
pop_and_print(h)
Example #4
0
def pop_all(h):
    l = []
    while h:
        l.append(heapq.heappop(h, True))
    dprint(l)
    return l
Example #5
0
else:
    def dprint(*v):
        pass

# Try not to crash on invalid data
h = []
heapq.heappush(h, 1)
try:
    heapq.heappush(h, 2, True)
    assert False
except TypeError:
    pass

heapq.heappush(h, 2)
try:
    heapq.heappop(h, True)
    assert False
except TypeError:
    pass


def pop_all(h):
    l = []
    while h:
        l.append(heapq.heappop(h, True))
    dprint(l)
    return l

def add(h, v):
    heapq.heappush(h, (v, None), True)