예제 #1
0
    def test_buf_overfull(self):
        c = Chan(5)
        quickthread(sayset, c, list(range(20)), delay=0)
        time.sleep(0.1)  # Fill up buffer

        results = list(c)
        self.assertEqual(results, list(range(20)))
예제 #2
0
def timer(duration):
    def timer_thread(chan, duration):
        time.sleep(duration)
        chan.put(time.time())
    c = Chan()
    quickthread(timer_thread, c, duration)
    return c
예제 #3
0
    def test_buf_overfull(self):
        c = Chan(5)
        quickthread(sayset, c, list(range(20)), delay=0)
        time.sleep(0.1)  # Fill up buffer

        results = list(c)
        self.assertEqual(results, list(range(20)))
예제 #4
0
def timer(duration):
    def timer_thread(chan, duration):
        time.sleep(duration)
        chan.put(time.time())

    c = Chan()
    quickthread(timer_thread, c, duration)
    return c
예제 #5
0
    def fan_in(*input_list):
        def forward(input, output):
            while True:
                output.put(input.get())

        c = Chan()
        for input in input_list:
            quickthread(forward, input, c)
        return c
예제 #6
0
    def fan_in(input1, input2):
        def forwarder(input, output):
            while True:
                output.put(input.get())

        c = Chan()
        quickthread(forwarder, input1, c)
        quickthread(forwarder, input2, c)
        return c
예제 #7
0
    def fan_in(*input_list):
        def forward(input, output):
            while True:
                output.put(input.get())

        c = Chan()
        for input in input_list:
            quickthread(forward, input, c)
        return c
예제 #8
0
    def fan_in(input1, input2):
        def forwarder(input, output):
            while True:
                output.put(input.get())

        c = Chan()
        quickthread(forwarder, input1, c)
        quickthread(forwarder, input2, c)
        return c
예제 #9
0
    def fan_in(input1, input2):
        c = Chan()

        def forward():
            while True:
                chan, value = chanselect([input1, input2], [])
                c.put(value)

        quickthread(forward)
        return c
예제 #10
0
    def test_simple(self):
        chan = Chan()
        results = []
        quickthread(accumulator, chan, results)

        chan.put("Hello")
        time.sleep(0.01)  # Technically unsafe

        self.assertEqual(len(results), 1)
        self.assertEqual(results[0], "Hello")
예제 #11
0
    def test_simple(self):
        chan = Chan()
        results = []
        quickthread(accumulator, chan, results)

        chan.put("Hello")
        time.sleep(0.01)  # Technically unsafe

        self.assertEqual(len(results), 1)
        self.assertEqual(results[0], "Hello")
예제 #12
0
    def fan_in(input1, input2):
        c = Chan()

        def forward():
            while True:
                chan, value = chanselect([input1, input2], [])
                c.put(value)

        quickthread(forward)
        return c
예제 #13
0
    def test_select_and_closed(self):
        a, b, c = [Chan() for _ in range(3)]
        out = Chan()
        quickthread(sayset, a, [0, 1, 2], delay=0.01, __name='sayset1')
        quickthread(sayset, b, [3, 4, 5], delay=0.01, __name='sayset2')
        quickthread(sayset, c, [6, 7, 8], delay=0.01, __name='sayset2')

        def fanin_until_closed(inchans, outchan):
            inchans = inchans[:]
            while inchans:
                try:
                    _, val = chanselect(inchans, [])
                    out.put(val)
                except ChanClosed as ex:
                    inchans.remove(ex.which)
            out.close()

        quickthread(fanin_until_closed, [a, b, c], out, __name='fanin')

        into = []
        acc = quickthread(accumulator, out, into)
        acc.join(10)
        self.assertFalse(acc.is_alive())

        results = set(into)
        self.assertEqual(len(results), 9)
        self.assertEqual(results, set(range(9)))
예제 #14
0
    def test_select_and_closed(self):
        a, b, c = [Chan() for _ in range(3)]
        out = Chan()
        quickthread(sayset, a, [0, 1, 2], delay=0.01, __name='sayset1')
        quickthread(sayset, b, [3, 4, 5], delay=0.01, __name='sayset2')
        quickthread(sayset, c, [6, 7, 8], delay=0.01, __name='sayset2')

        def fanin_until_closed(inchans, outchan):
            inchans = inchans[:]
            while inchans:
                try:
                    _, val = chanselect(inchans, [])
                    out.put(val)
                except ChanClosed as ex:
                    inchans.remove(ex.which)
            out.close()

        quickthread(fanin_until_closed, [a, b, c], out, __name='fanin')

        into = []
        acc = quickthread(accumulator, out, into)
        acc.join(10)
        self.assertFalse(acc.is_alive())

        results = set(into)
        self.assertEqual(len(results), 9)
        self.assertEqual(results, set(range(9)))
예제 #15
0
    def boring(message):
        def sender(message, c):
            i = 0
            while True:
                c.put("%s: %d" % (message, i))
                time.sleep(0.2 * random.random())
                i += 1

        c = Chan()
        quickthread(sender, message, c)
        return c
예제 #16
0
    def boring(msg):
        c = Chan()

        def sender():
            i = 0
            while True:
                c.put("%s: %d" % (msg, i))
                time.sleep(1.5 * random.random())
                i += 1
        quickthread(sender)
        return c
예제 #17
0
    def boring(message):
        def sender(message, c):
            i = 0
            while True:
                c.put("%s: %d" % (message, i))
                time.sleep(0.2 * random.random())
                i += 1

        c = Chan()
        quickthread(sender, message, c)
        return c
예제 #18
0
    def boring(msg):
        c = Chan()

        def sender():
            i = 0
            while True:
                c.put("%s: %d" % (msg, i))
                time.sleep(1.5 * random.random())
                i += 1

        quickthread(sender)
        return c
예제 #19
0
    def boring(msg):
        c = Chan()
        wait_for_it = Chan()

        def sender():
            i = 0
            while True:
                c.put(Message("%s: %d" % (msg, i), wait_for_it))
                time.sleep(0.2 * random.random())
                wait_for_it.get()
                i += 1
        quickthread(sender)
        return c
예제 #20
0
    def test_iter_and_closed(self):
        c = Chan()
        quickthread(sayset, c, [1, 2, 3], delay=0)

        def listener():
            it = iter(c)
            self.assertEqual(next(it), 1)
            self.assertEqual(next(it), 2)
            self.assertEqual(next(it), 3)
            self.assertRaises(StopIteration, it.__next__)
        t = quickthread(listener)

        time.sleep(0.1)
        self.assertFalse(t.is_alive())
예제 #21
0
    def boring(msg):
        c = Chan()
        wait_for_it = Chan()

        def sender():
            i = 0
            while True:
                c.put(Message("%s: %d" % (msg, i), wait_for_it))
                time.sleep(0.2 * random.random())
                wait_for_it.get()
                i += 1

        quickthread(sender)
        return c
예제 #22
0
    def boring(msg, quit):
        c = Chan()

        def sender():
            i = 0
            while True:
                time.sleep(1.0 * random.random())

                chan, _ = chanselect([quit], [(c, "%s: %d" % (msg, i))])
                if chan == quit:
                    quit.put("See you!")
                i += 1
        quickthread(sender)
        return c
예제 #23
0
    def boring(msg, quit):
        c = Chan()

        def sender():
            i = 0
            while True:
                time.sleep(1.0 * random.random())

                chan, _ = chanselect([quit], [(c, "%s: %d" % (msg, i))])
                if chan == quit:
                    quit.put("See you!")
                i += 1

        quickthread(sender)
        return c
예제 #24
0
    def test_iter_and_closed(self):
        c = Chan()
        quickthread(sayset, c, [1, 2, 3], delay=0)

        def listener():
            it = iter(c)
            self.assertEqual(next(it), 1)
            self.assertEqual(next(it), 2)
            self.assertEqual(next(it), 3)
            self.assertRaises(StopIteration, it.__next__)

        t = quickthread(listener)

        time.sleep(0.1)
        self.assertFalse(t.is_alive())
예제 #25
0
    def test_nothing_lost(self):
        phrases = ['Hello_%03d' % x for x in range(1000)]
        firstchan = Chan()
        chan_layer1 = [Chan() for i in range(6)]
        lastchan = Chan()
        sayer = quickthread(sayset, firstchan, phrases, delay=0.001,
                            __name='sayer')

        # Distribute firstchan -> chan_layer1
        for i in range(12):
            outchans = [chan_layer1[(i+j) % len(chan_layer1)]
                        for j in range(3)]
            quickthread(distributer, [firstchan], outchans, delay_max=0.005,
                        __name='dist_layer1_%02d' % i)

        # Distribute chan_layer1 -> lastchan
        for i in range(12):
            inchans = [chan_layer1[(i+j) % len(chan_layer1)]
                       for j in range(0, 9, 3)]
            quickthread(distributer, inchans, [lastchan], delay_max=0.005,
                        __name='dist_layer2_%02d' % i)

        results = []
        quickthread(accumulator, lastchan, results, __name='accumulator')
        sayer.join(10)
        self.assertFalse(sayer.is_alive())
        time.sleep(1)  # Unsafe.  Lets the data propagate to the accumulator

        # Checks that none are missing, and there are no duplicates.
        self.assertEqual(len(results), len(phrases))
        self.assertEqual(set(results), set(phrases))
예제 #26
0
def example_daisy():
    def f(left, right):
        left.put(1 + right.get())

    N = 1000  # Python's threads aren't that lightweight
    leftmost = Chan()
    rightmost = leftmost
    left = leftmost
    for i in xrange(N):
        right = Chan()
        quickthread(f, left, right)
        left = right

    def putter():
        right.put(1)
    quickthread(putter)

    print leftmost.get()
예제 #27
0
def example_daisy():
    def f(left, right):
        left.put(1 + right.get())

    N = 1000  # Python's threads aren't that lightweight
    leftmost = Chan()
    rightmost = leftmost
    left = leftmost
    for i in xrange(N):
        right = Chan()
        quickthread(f, left, right)
        left = right

    def putter():
        right.put(1)

    quickthread(putter)

    print leftmost.get()
예제 #28
0
def main():
    FetcherCls = MockFetcher

    merged = Merged([
        Subscription(FetcherCls('blog.golang.org')),
        Subscription(FetcherCls('googleblog.blogspot.com')),
        Subscription(FetcherCls('googledevelopers.blogspot.com'))])

    # Close after a while
    def close_later():
        time.sleep(3)
        print("Closed: {}".format(merged.close()))
    quickthread(close_later)

    for it in merged.updates():
        print("{} -- {}".format(it.channel, it.title))

    time.sleep(0.1)
    print("Still active:         (should only be _MainThread and timeouts)")
    for active in threading._active.itervalues():
        print("    {}".format(active))
예제 #29
0
def main():
    FetcherCls = MockFetcher

    merged = Merged([
        Subscription(FetcherCls('blog.golang.org')),
        Subscription(FetcherCls('googleblog.blogspot.com')),
        Subscription(FetcherCls('googledevelopers.blogspot.com'))
    ])

    # Close after a while
    def close_later():
        time.sleep(3)
        print("Closed: {}".format(merged.close()))

    quickthread(close_later)

    for it in merged.updates():
        print("{} -- {}".format(it.channel, it.title))

    time.sleep(0.1)
    print("Still active:         (should only be _MainThread and timeouts)")
    for active in threading._active.itervalues():
        print("    {}".format(active))
예제 #30
0
    def test_nothing_lost(self):
        phrases = ['Hello_%03d' % x for x in range(1000)]
        firstchan = Chan()
        chan_layer1 = [Chan() for i in range(6)]
        lastchan = Chan()
        sayer = quickthread(sayset,
                            firstchan,
                            phrases,
                            delay=0.001,
                            __name='sayer')

        # Distribute firstchan -> chan_layer1
        for i in range(12):
            outchans = [
                chan_layer1[(i + j) % len(chan_layer1)] for j in range(3)
            ]
            quickthread(distributer, [firstchan],
                        outchans,
                        delay_max=0.005,
                        __name='dist_layer1_%02d' % i)

        # Distribute chan_layer1 -> lastchan
        for i in range(12):
            inchans = [
                chan_layer1[(i + j) % len(chan_layer1)]
                for j in range(0, 9, 3)
            ]
            quickthread(distributer,
                        inchans, [lastchan],
                        delay_max=0.005,
                        __name='dist_layer2_%02d' % i)

        results = []
        quickthread(accumulator, lastchan, results, __name='accumulator')
        sayer.join(10)
        self.assertFalse(sayer.is_alive())
        time.sleep(1)  # Unsafe.  Lets the data propagate to the accumulator

        # Checks that none are missing, and there are no duplicates.
        self.assertEqual(len(results), len(phrases))
        self.assertEqual(set(results), set(phrases))
예제 #31
0
 def test_buf_kept_empty(self):
     c = Chan(5)
     quickthread(sayset, c, list(range(20)), delay=0.02)
     results = list(c)
     self.assertEqual(results, list(range(20)))
예제 #32
0
 def test_buf_kept_empty(self):
     c = Chan(5)
     quickthread(sayset, c, list(range(20)), delay=0.02)
     results = list(c)
     self.assertEqual(results, list(range(20)))