Example #1
0
def pipeline():
    files = goless.chan()
    hashes = goless.chan()
    results = goless.chan()

    def scanner():
        for d, dn, f in os.walk('.'):
            for fn in f:
                files.send(os.path.join(d,fn))
        files.close()

    def hasher():
        for f in files:
            with open(f, 'rb') as fd:
                md5 = hashlib.md5(fd.read()).hexdigest()
                hashes.send((f, md5))
        hashes.close()

    def collector():
        for f, md5 in hashes:
            results.send((f, md5))
        results.close()

    goless.go(scanner)
    goless.go(hasher)
    goless.go(collector)

    for filename, md5hash in results:
        print('%s: %s' % (filename, md5hash))
Example #2
0
def run():
    channels = {
        'c1': goless.chan(0),
        'c2': goless.chan(0),
        'c3': goless.chan(0)
    }
    workers = [make_worker(logme, channels['c1'], channels['c2']), 
               make_worker(logme, channels['c2'], channels['c3'])]

    logging.info("run: defining send_messages...")
    print("run: defining send_messages...")
    def send_messages():
        for ii in range(10):
            print("sending val={} into channel['c1']={}", val, channels['c1'])
            val = "val:{}".format(ii)
            channels['c1'].send(val)

    res = goless.go(send_messages)
    print("called goless.go(send_messages) => {}".format(res))

    while True:
        cases = [goless.rcase(channels['c3']),
                 goless.dcase()]
        case, val = goless.select(cases)
        if case == cases[1]:
            print("hit default, perhaps things are closed? case={} val={}".format(case, val))
            break
        print("case={}; c3 => {}".format(case, val))
        print("closing channel c3")
        channels['c3'].close()
        print("exiting")
Example #3
0
def main():
    done = goless.chan()
    msgs = goless.chan()
    out = goless.chan()

    def produce():
        for i in range(10):
            msgs.send(i)
        msgs.close()

    def consume(name):
        for msg in msgs:
            out.send('%s:%s ' % (name, msg))
        out.close()

    def logger():
        for msg in out:
            sys.stdout.write(msg)
        sys.stdout.write('\n')
        done.send()

    goless.go(produce)
    goless.go(consume, "one")
    goless.go(consume, "two")
    goless.go(logger)
    done.recv()
Example #4
0
 def test_select_chooses_ready_selection(self):
     readychan = goless.chan(1)
     notreadychan = goless.chan(1)
     readychan.send(3)
     cases = [goless.rcase(notreadychan), goless.rcase(readychan), goless.dcase()]
     result, val = goless.select(cases)
     self.assertIs(result, cases[1])
     self.assertEqual(val, 3)
Example #5
0
 def test_select_raises_for_list_and_args(self):
     chan1 = goless.chan(1)
     chan2 = goless.chan(1)
     chan3 = goless.chan(1)
     cases = [goless.scase(chan1, 1), goless.scase(chan2, 2)]
     
     with self.assertRaises(TypeError):
         goless.select(cases, chan3)
Example #6
0
    def test_select_no_default_no_ready_blocks(self):
        chan1 = goless.chan()
        chan2 = goless.chan()
        a = []
        cases = [goless.rcase(chan2), goless.rcase(chan1)]

        def sel():
            a.append(goless.select(cases))
        be.run(sel)
        self.assertEqual(a, [])
        chan1.send(5)
        be.yield_()
        self.assertEqual(len(a), 1)
        chosen, val = a[0]
        self.assertEqual(chosen, cases[1])
        self.assertEqual(val, 5)
Example #7
0
 def test_range_over_channels(self):
     # https://gobyexample.com/range-over-channels
     queue = goless.chan(2)
     queue.send('one')
     queue.send('two')
     queue.close()
     elements = [elem for elem in queue]
     self.assertEqual(elements, ['one', 'two'])
Example #8
0
    def integers():
        yielder = goless.chan()

        def counter():
            count = 0
            while True:
                yielder.send(count)
                count += 1

        goless.go(counter)
        return yielder
Example #9
0
 def test_case_switch(self):
     chan = goless.chan()
     cases = [goless.rcase(chan), goless.scase(chan, 1), goless.dcase()]
     chosen, value = goless.select(cases)
     if chosen is cases[0]:
         print('Received %s' % value)
     elif chosen is cases[1]:
         assert value is None
         print('Sent.')
     else:
         assert chosen is cases[2], chosen
         print('Default...')
Example #10
0
def simple():
    channel = goless.chan()

    def goroutine():
        while True:
            value = channel.recv()
            channel.send(value ** 2)
    goless.go(goroutine)

    for i in range(2, 5):
        channel.send(i)
        squared = channel.recv()
        print('%s squared is %s' % (i, squared))
Example #11
0
    def test_select(self):
        # https://gobyexample.com/select
        c1 = goless.chan()
        c2 = goless.chan()

        def func1():
            time.sleep(.1)
            c1.send('one')
        goless.go(func1)

        def func2():
            time.sleep(.2)
            c2.send('two')
        goless.go(func2)

        # We don't print since we run this as a test.
        callbacks = []

        for i in range(2):
            _, val = goless.select([goless.rcase(c1), goless.rcase(c2)])
            callbacks.append(val)

        self.assertEqual(callbacks, ['one', 'two'])
Example #12
0
    def test_worker_pool(self):
        # https://gobyexample.com/worker-pools
        jobs_done = []

        # noinspection PyShadowingNames,PyShadowingBuiltins
        def worker(id, jobs, results):
            for j in jobs:
                jobs_done.append('w %s j %s' % (id, j))
                time.sleep(.01)
                results.send(j * 2)

        jobs = goless.chan(100)
        results = goless.chan(100)

        for w in range(1, 4):
            goless.go(lambda: worker(w, jobs, results))

        for j in range(1, 10):
            jobs.send(j)
        jobs.close()

        for a in range(1, 10):
            results.recv()
        self.assertEqual(len(jobs_done), 9)
Example #13
0
def pfor():
    n = 10
    items = range(n)
    results = [None] * n
    semaphore = goless.chan(n)

    def mapper(index, value):
        results[index] = dosomething(value)
        semaphore.send()

    for i, item in enumerate(items):
        mapper(i, item)
    for _ in range(n):
        semaphore.recv()
    print('Finished: %s' % results)
Example #14
0
def bench_channel(chan_size):
    c = chan(chan_size)

    def func():
        for _ in xrange(QUEUE_LEN):
            c.send(0)
        c.close()
    count = 0

    go(func)
    start = time.clock()
    for _ in xrange(QUEUE_LEN):
        c.recv()
        count += 1
    end = time.clock()
    return end - start
Example #15
0
    def test_successful_recv_does_yield_control(self):
        """Test that send with a waiting receiver
        *does* give control to the waiting receiver."""
        chan = goless.chan()
        actions = []

        def other():
            actions.append('send pending')
            chan.send()
            actions.append('send acted')

        goless.go(other)
        actions.append('recv pending')
        chan.recv()
        actions.append('recv acted')

        self.assertEqual(actions, [
            'recv pending',
            'send pending',
            'recv acted',
        ])
Example #16
0
def bench_select(use_default):
    c = chan(0)
    cases = [
        selecting.scase(c, 1),
        selecting.rcase(c),
        selecting.scase(c, 1),
        selecting.rcase(c),
    ]
    if use_default:
        cases.append(selecting.dcase())

    def sender():
        while True:
            c.send(0)
            c.recv()
    go(sender)

    start = time.clock()
    for i in xrange(QUEUE_LEN):
        selecting.select(cases)
    end = time.clock()
    return end - start
Example #17
0
    def test_successful_recv_continues(self):
        """Test that recv with a waiting sender
        *does not* give control to the waiting sender."""
        chan = goless.chan()
        actions = []

        def other():
            actions.append('recv pending')
            chan.recv()
            actions.append('recv acted')

        goless.go(other)
        actions.append('send pending')
        chan.send()
        actions.append('send acted')

        self.assertEqual(actions, [
            'send pending',
            'recv pending',
            'recv acted',
            'send acted',
        ])
Example #18
0
 def test_main_tasklet_can_select(self):
     chan1 = goless.chan(1)
     cases = [goless.scase(chan1, 3)]
     chosen, val = goless.select(cases)
     self.assertIs(chosen, cases[0])
     self.assertIsNone(val)
Example #19
0
import goless, time

channels = []
channels.append(goless.chan())
channels.append(goless.chan())
channels.append(goless.chan())

def makeWorker(params):
  name = params['name']
  def func1():
      print("{}[{}] waiting for val".format(name, params['val']))
      case, val = goless.select([goless.rcase(params['cin'])])
      print("{}[{}] got case={}, val={}".format(name, params['val'], case, val))
      time.sleep(params['sleep'])
      print("{}[{}] sending to cout...".format(name, params['val']))
      params['cout'].send("(" + name + ")=" + val + ";" + params['val'])
  return func1

funcs = [makeWorker({
  'name': 'workerA',
  'cin':  channels[0],
  'cout': channels[1],
  'val':  'one',
  'sleep': 1
  }), 
  makeWorker({
  'name': 'workerB',
  'cin':  channels[1],
  'cout': channels[2],
  'val':  'two',
  'sleep': 1
Example #20
0
 def test_select_accepts_args(self):
     chan1 = goless.chan(1)
     scase = goless.scase(chan1, 1)
     chosen, val = goless.select(scase)
     self.assertIs(chosen, scase)
     self.assertIsNone(val)
Example #21
0
 def test_raises_deadlock_if_no_goroutines(self):
     with self.assertRaises(goless.Deadlock):
         goless.select(goless.rcase(goless.chan()))
Example #22
0
def promise(value):
    future = goless.chan(1)
    future.send(value)
    return future
Example #23
0
def product_async(future_a, future_b):
    c = goless.chan()
    goless.go(lambda: c.send(future_a.recv() * future_b.recv()))
    return c
Example #24
0
def inverse_async(future):
    c = goless.chan()
    goless.go(lambda: c.send(future.recv() * -1))
    return c
Example #25
0
 def setUp(self):
     BaseTests.setUp(self)
     self.ch = goless.chan(self.chansize)
     self.ca = goless.rcase(self.ch)
Example #26
0
 def setUp(self):
     BaseTests.setUp(self)
     self.ch = goless.chan(self.chansize)
     self.sendval = 1
     self.ca = goless.scase(self.ch, self.sendval)
Example #27
0
 def setUp(self):
     BaseTests.setUp(self)
     self.chan1 = goless.chan()