Beispiel #1
0
    def __init__(self, subscriptions):
        self.subscriptions = subscriptions
        self.updates_chan = Chan()
        self.quit = Chan()

        self.thread = threading.Thread(name="Merged", target=self._run)
        self.thread.start()
Beispiel #2
0
 def __init__(self, fetcher):
     self.fetcher = fetcher
     self.updates_chan = Chan()
     self.quit = Chan()
     self.thread = threading.Thread(name='Subscription', target=self._run)
     #self.thread.daemon = True
     self.thread.start()
Beispiel #3
0
    def test_select_and_closed(self):
        a, b, c = [Chan() for _ in range(3)]
        out = Chan()
        go(sayset, a, [0, 1, 2], delay=0.01, __name='sayset1')
        go(sayset, b, [3, 4, 5], delay=0.01, __name='sayset2')
        go(sayset, c, [6, 7, 8], delay=0.01, __name='sayset2')

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

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

        into = []
        acc = go(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)))
Beispiel #4
0
def write_write_default():
    """Two channels can be read and there's a default clause
    Expected output :
    -----------------
    Writing (42|51) to ch(1|2)
    Writing (42|51) to ch(1|2)
    Received number (42|51) from ch(1|2)
    """

    ch1A = Chan()
    ch1B = Chan()
    ch2A = Chan()
    ch2B = Chan()

    def read_channel1():
        """Read the value in ch1A and write it in ch1B"""
        py_output.write('Reading from ch1\n'.encode())
        x = ch1A.get()
        ch1B.put(x)

    def read_channel2():
        """Read the value in ch2A and write it in ch2B"""
        py_output.write('Reading from ch2\n'.encode())
        x = ch2A.get()
        ch2B.put(x)

    go(read_channel1)
    go(read_channel2)

    time.sleep(0.1)
    # Hopefully this is enough time to let
    # the other two goroutines block on their channel writes

    nb1 = 42
    nb2 = 51
    sent = 0

    chan, nb = select(consumers=[],
                      producers=[(ch1A, 42), (ch2A, 51)],
                      default=True)
    if chan == ch1A:
        py_output.write('Sent number 42 to ch2\n'.encode())
        sent = nb1
    elif chan == ch2A:
        py_output.write('Sent number 51 to ch2\n'.encode())
        sent = nb2
    elif chan == 'default':
        raise Exception('Selected default behavior')

    if sent == 42:  # ch1.put(42) happened
        ch2A.put(17)
        assert ch1B.get() == 42, "We should be able to read 42 from ch1B"
    elif sent == 51:  # ch2.put(51) happened
        ch1A.put(17)
        assert ch2B.get() == 51, "We should be able to read 51 from ch2B"
    else:
        raise Exception('WTF ?')
Beispiel #5
0
    def test_select_timeout(self):
        a = Chan()
        b = Chan()
        c = Chan()
        self.assertRaises(Timeout, select, [a, b], [(c, 42)], timeout=0)
        self.assertRaises(Timeout, select, [a, b], [(c, 42)], timeout=0.01)

        # Verifies that select didn't leave any wishes lying around.
        self.assertRaises(Timeout, a.put, 12, timeout=0)
        self.assertRaises(Timeout, c.get, timeout=0)
Beispiel #6
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

        go(sender)
        return c
Beispiel #7
0
 def test_putget_timeout(self):
     c = Chan()
     self.assertRaises(Timeout, c.put, 'x', timeout=0)
     self.assertRaises(Timeout, c.put, 'x', timeout=0.01)
     self.assertRaises(Timeout, c.get, timeout=0)
     self.assertRaises(Timeout, c.get, timeout=0.01)
     self.assertRaises(Timeout, c.put, 'x', timeout=0)
Beispiel #8
0
def update():
    global itemK,itemBi,itemLine,itemZhongshu
    dataToNowDf = DataFrame(index=dataToNow.Times,data = dataToNow.Data[0],columns=['price'])
    dataToNowDf = dataToNowDf.between_time('9:30','11:30').append(dataToNowDf.between_time('13:00','15:00'))
    a = dataToNowDf.resample('30T',how = {'price':'ohlc'},label='right').dropna()
    for i in a.iterrows():
        data.Times.append(i[0].to_datetime())
        data.Data[0].append(i[1]['price']['open'])
        data.Data[1].append(i[1]['price']['high'])
        data.Data[2].append(i[1]['price']['low'])
        data.Data[3].append(i[1]['price']['close'])
        data.Data[4].append(0)
    quotes = []
    for i in range(len(data.Times)):
        quotes.append([i, data.Data[0][i], data.Data[3][i],
                       data.Data[2][i], data.Data[1][i]])
     
    chan = Chan(data.Data[0], data.Data[1], data.Data[2],
                data.Data[3], data.Data[4], data.Times)
    chan.barsMerge()
    chan.findFenxing()
    chan.findBi()
    chan.findLines()
    chan.findZhongshus()
    chan.calculate_ta()
    a += 1
    itemK.set_data(quotes)
#    itemBi.set_data(chan.bis)
#    itemLine.set_data(chan.lines)
#    itemZhongshu.set_data(chan.zhongshus)
    app.processEvents()  ## force complete redraw for every plot
Beispiel #9
0
    def test_buf_overfull(self):
        c = Chan(5)
        go(sayset, c, list(range(20)), delay=0)
        time.sleep(0.1)  # Fill up buffer

        results = list(c)
        self.assertEqual(results, list(range(20)))
Beispiel #10
0
    def play(self):
        '''round-robin'''
        self.open_start_urls()
        loops_without_progress = 0
        while True:
            import time
            start = time.time()
            if len(self.bots) == 0:
                return
            # bots got stuck if there's 2 wait pages in a row
            if loops_without_progress > 10:
                raise AssertionError('Bots got stuck')
            results = Chan(buflen=len(self.bots))
            threads = []
            for bot in self.bots.values():
                if bot.on_wait_page():
                    pass
                else:
                    thread = SubmitThread(bot, results)
                    threads.append(thread)
                    thread.start()

            for thread in threads:
                bot, status = results.get()
                if isinstance(status, Exception):
                    raise status
                elif status == 'finished':
                    del self.bots[bot.participant.id]
                else:
                    bot.submit(status)
            results.close()

            if not threads:
                loops_without_progress += 1
Beispiel #11
0
 def close(self):
     errc = Chan()
     self.quit.put(errc)
     result = errc.get()
     self.thread.join(0.2)
     assert not self.thread.is_alive()
     return result
Beispiel #12
0
def read_write_default():
    """One channel can be read, the written and there's a default clause
    Expected output :
    -----------------
    (Writing 42 to ch1|Reading from ch2)
    (Writing 42 to ch1|Reading from ch2)
    (Received number 42 from ch1|Sent number 51 to ch2)
    """

    ch1 = Chan()
    ch2 = Chan()

    def write_channel():
        """Write 42 into ch1"""
        py_output.write('Writing 42 to ch1\n'.encode())
        ch1.put(42)

    def read_channel():
        """Read the value in ch2 and write it in ch1"""
        py_output.write('Reading from ch2\n'.encode())
        x = ch2.get()
        ch1.put(x)

    go(write_channel)
    go(read_channel)

    time.sleep(0.1)
    # Hopefully this is enough time to let
    # the other two goroutines block on their channel writes

    nb = 51

    chan, nb = select(consumers=[ch1], producers=[(ch2, 51)], default=True)
    if chan == ch1:
        py_output.write('Received number {} from ch1\n'.format(nb).encode())
    elif chan == ch2:
        py_output.write('Sent number 51 to ch2\n'.encode())
    elif chan == 'default':
        raise Exception('Selected default behavior')

    if nb == 42:  # ch1.get() happened
        ch2.put(17)
        assert ch1.get() == 17, "We should be able to read 17 from ch1"
    elif nb is None:  # ch2.put(51) happened
        assert ch1.get() == 42, "We should be able to read '42' from ch1"
    else:
        raise Exception('WTF ?')
Beispiel #13
0
def timer(duration):
    def timer_thread(chan, duration):
        time.sleep(duration)
        chan.put(time.time())

    c = Chan()
    go(timer_thread, c, duration)
    return c
Beispiel #14
0
 def __init__(self, period=DEFAULT_PERIOD, yellow=DEFAULT_YELLOW):
     self.period = period
     self.yellow = yellow
     self.trip = False
     self._stop = threading.Event()
     self.chan = Chan()
     self._thread = threading.Thread(name='LightTimer', target=self.run)
     self._thread.daemon = True
     self._thread.start()
Beispiel #15
0
    def fan_in(*input_list):
        def forward(input, output):
            while True:
                output.put(input.get())

        c = Chan()
        for input in input_list:
            go(forward, input, c)
        return c
Beispiel #16
0
    def fan_in(input1, input2):
        def forwarder(input, output):
            while True:
                output.put(input.get())

        c = Chan()
        go(forwarder, input1, c)
        go(forwarder, input2, c)
        return c
Beispiel #17
0
 def __init__(self, color, blink=False, period=0.5):
     self.blink = blink
     self.period = period
     self.color = color.split('-')[0]
     self.chan = Chan()
     self._stop = threading.Event()
     self._thread = threading.Thread(name='LightTimer', target=self.run)
     self._thread.daemon = True
     self._thread.start()
Beispiel #18
0
    def test_buf_simple(self):
        S = 5
        c = Chan(S)
        for i in range(S):
            c.put(i)
        c.close()

        results = list(c)
        self.assertEqual(results, list(range(S)))
Beispiel #19
0
def timeout_after(delay):
    def thread(ch):
        time.sleep(delay)
        ch.put(None)

    c = Chan()
    t = threading.Thread(name='timeout', target=thread, args=(c, ))
    t.daemon = True
    t.start()
    return c
Beispiel #20
0
def read_read_default():
    """Two channels can be read and there's a default clause
    Expected output :
    -----------------
    Writing (42|51) to ch(1|2)
    Writing (42|51) to ch(1|2)
    Received number (42|51) from ch(1|2)
    """

    ch1 = Chan()
    ch2 = Chan()

    def write_channel1():
        """Write 42 into ch1"""
        py_output.write('Writing 42 to ch1\n'.encode())
        ch1.put(42)

    def write_channel2():
        """Write 51 into ch2"""
        py_output.write('Writing 51 to ch2\n'.encode())
        ch2.put(51)

    go(write_channel1)
    go(write_channel2)

    time.sleep(0.1)
    # Hopefully this is enough time to let
    # the other two goroutines block on their channel writes

    chan, nb = select(consumers=[ch1, ch2], producers=[], default=True)
    if chan == ch1:
        py_output.write('Received number {} from ch1\n'.format(nb).encode())
    elif chan == ch2:
        py_output.write('Received number {} from ch2\n'.format(nb).encode())
    elif chan == 'default':
        raise Exception('Selected default behavior')

    if nb == 42:  # ch1.get() happened
        assert ch2.get() == 51, "We should be able to read '51' from ch2"
    elif nb == 51:  # ch2.get() happened
        assert ch1.get() == 42, "We should be able to read '42' from ch1"
    else:
        raise Exception('WTF ?')
Beispiel #21
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()
        go(f, left, right)
        left = right

    def putter():
        right.put(1)

    go(putter)

    print leftmost.get()
Beispiel #22
0
    def test_simple(self):
        chan = Chan()
        results = []
        go(accumulator, chan, results)

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

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

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

        quickthread(forward)
        return c
Beispiel #24
0
 def __init__(self, base=1000, jitter=500):
     self.trip = False
     self._stop = threading.Event()
     self.chan = Chan()
     self._thread = threading.Thread(name='LightTimer', target=self.run)
     self._thread.daemon = True
     self._thread.start()
     self.base = base
     self.jitter = jitter
     random.seed()
Beispiel #25
0
 def status(self):
     st = {'server': 'ok'}
     try:
         resp_chan = Chan(1)
         self.chan.put(('status', resp_chan), timeout=0.5)
         resp = resp_chan.get(timeout=0.5)
         st.update(resp)
     except Timeout:
         st['loop'] = 'Error: Did not hear from main thread in time'
     return st
Beispiel #26
0
    def fan_in(input1, input2):
        c = Chan()

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

        go(forward)
        return c
Beispiel #27
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()
        go(sender, message, c)
        return c
Beispiel #28
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))
Beispiel #29
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

        go(sender)
        return c
Beispiel #30
0
def read_default():
    """The select should execute the default code as many times as needed
    (hopefully once)
    Then the <-ch1 case should happen
    Expected output :
    -----------------
    Selected default behavior
    Writing 42 to ch1
    Received number 42 from ch1 after 1[0-9].[0-9]*µs
    """

    ch1 = Chan()
    ch2 = Chan()

    def write_channel(ch):
        """Wait for ch to be readable and Write an int nb
        into a channel ch"""
        ch.get()
        py_output.write('Writing 42 to ch1\n'.encode())
        ch1.put(42)

    go(write_channel, ch2)

    from_ch1 = 0
    wrote_to_ch2 = False
    start = time.time()
    while from_ch1 == 0:
        chan, nb = select(consumers=[ch1], producers=[], default=True)
        if chan == ch1:
            elapsed = time.time() - start
            from_ch1 = nb
            py_output.write('Received number '
                            '{} from ch1 after '
                            '{}\n'.format(from_ch1, elapsed).encode())
        if chan is 'default':
            py_output.write('Selected default behavior\n'.encode())
            if not wrote_to_ch2:
                ch2.put(1)
                wrote_to_ch2 = True