Esempio n. 1
0
    def test_exc(self):
        def raiseit():
            raise RuntimeError("ha!")

        goless.go(raiseit)
        be.yield_()
        self.assertEqual(len(self.panic_calls), 1)
Esempio n. 2
0
    def test_exc(self):
        def raiseit():
            raise RuntimeError('ha!')

        goless.go(raiseit)
        be.yield_()
        self.assertEqual(len(self.panic_calls), 1)
Esempio n. 3
0
    def integers():
        yielder = goless.chan()

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

        goless.go(counter)
        return yielder
Esempio n. 4
0
    def test_behavior(self):
        chan = gochans.SyncChannel()
        results = []

        goless.go(lambda: chan.send(1))

        def check_results_empty():
            self.assertFalse(results)
            chan.send(2)
        goless.go(check_results_empty)

        results = [chan.recv(), chan.recv()]
        self.assertEqual(results, [1, 2])
Esempio n. 5
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))
Esempio n. 6
0
    def test_behavior(self):
        chan = gochans.SyncChannel()
        results = []

        goless.go(lambda: chan.send(1))

        def check_results_empty():
            self.assertFalse(results)
            chan.send(2)
        goless.go(check_results_empty)

        results = [chan.recv(), chan.recv()]
        self.assertEqual(results, [1, 2])
Esempio n. 7
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))
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()
Esempio n. 9
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):
        goless.go(mapper, i, item)
    for _ in range(n):
        semaphore.recv()
    print('Finished: %s' % results)
Esempio n. 10
0
def migrate_worker(setting):
    """ Migrates the populations and sends them to the migrated channel. """

    population_a = None
    population_b = None
    while True:
        population_a = population_a or evolved.recv()
        population_b = evolved.recv() if population_b else population_a
        population = crossover_migration(population_a['population'],
                                         population_b['population'])
        redis_logs.send(population_a)
        migrated.send(population_a)
        population_a = population_b
        parameters = create_parameters(settings, population)
        go(populations.send, parameters)
Esempio n. 11
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
Esempio n. 12
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")
Esempio n. 13
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
Esempio n. 14
0
    def test_recv_and_send_with_room_do_not_block(self):
        resultschan = gochans.BufferedChannel(5)
        endchan = gochans.SyncChannel()

        def square(x):
            return x * x

        def func():
            for num in range(5):
                resultschan.send(square(num))
            endchan.send()

        goless.go(func)
        # Waiting on the endchan tells us our results are
        # queued up in resultschan
        endchan.recv()
        got = [resultschan.recv() for _ in range(5)]
        ideal = [square(i) for i in range(5)]
        self.assertEqual(got, ideal)
Esempio n. 15
0
    def test_recv_and_send_with_room_do_not_block(self):
        resultschan = gochans.BufferedChannel(5)
        endchan = gochans.SyncChannel()

        def square(x):
            return x * x

        def func():
            for num in range(5):
                resultschan.send(square(num))
            endchan.send()

        goless.go(func)
        # Waiting on the endchan tells us our results are
        # queued up in resultschan
        endchan.recv()
        got = [resultschan.recv() for _ in range(5)]
        ideal = [square(i) for i in range(5)]
        self.assertEqual(got, ideal)
Esempio n. 16
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',
        ])
Esempio n. 17
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))
Esempio n. 18
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))
Esempio n. 19
0
    def _successful_op_does_not_yield_control(self, thisop, otherop):
        chan = self.makechan()
        actions = []

        def other():
            actions.append('other pending')
            getattr(chan, otherop)()
            actions.append('other acted')

        actions.append('other start')
        goless.go(other)
        actions.append('this pending')
        getattr(chan, thisop)()
        actions.append('this acted')

        self.assertEqual(actions, [
            'other start',
            'this pending',
            'other pending',
            'other acted',
            'this acted'
        ])
Esempio n. 20
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',
        ])
Esempio n. 21
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
Esempio n. 22
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
Esempio n. 23
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'])
Esempio n. 24
0
def make_worker(workerfn, cin, cout, startWorker=True):
    def worker():
        while True:
            case, val = goless.select([goless.rcase(cin)])
            if val is None:
                print("cin returned None, is it closed?")
                cout.close()
                return
            print("f1: case={}, val={}".format(case, val))
            cout.send(workerfn(val))
    if startWorker:
        res = goless.go(worker)
        print("started worker: {} => {}".format(worker, res))
    return worker
Esempio n. 25
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'])
Esempio n. 26
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)
Esempio n. 27
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)
Esempio n. 28
0
      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
    })]

for f in funcs:
  goless.go(f)

channels[0].send('(main)=inval')

for i in range(1):
    case, val = goless.select([goless.rcase(channels[2])])
    print("result={}".format(val))

Esempio n. 29
0
def product_async(future_a, future_b):
    c = goless.chan()
    goless.go(lambda: c.send(future_a.recv() * future_b.recv()))
    return c
Esempio n. 30
0
def inverse_async(future):
    c = goless.chan()
    goless.go(lambda: c.send(future.recv() * -1))
    return c
Esempio n. 31
0
def product_async(future_a, future_b):
    c = goless.chan()
    goless.go(lambda: c.send(future_a.recv() * future_b.recv()))
    return c
Esempio n. 32
0
def inverse_async(future):
    c = goless.chan()
    goless.go(lambda: c.send(future.recv() * -1))
    return c
Esempio n. 33
0
 def test_starts_with_params(self):
     called = mock.Mock()
     goless.go(called, 10, a=1)
     be.yield_()
     called.assert_called_once_with(10, a=1)
Esempio n. 34
0
 def recieve(self):
     if not self.__pause:
         for connection in self.__connections.values():
             go(connection.foward)
         return self.__last.recieve()
Esempio n. 35
0
   oidList = [o.keys() for o in oids]
   for errorIndication, errorStatus, errorIndex, varBinds in nextCmd(SnmpEngine(),
           CommunityData('public'),
           UdpTransportTarget((ip, 161)),
           ContextData(),
           [ObjectType(ObjectIdentity('x')) for x in oidList],
           lexicographicMode=False):
       if errorIndication:
           print "Error: ", errorIndication
           break
       elif errorStatus:
           print ('%s at %s' % (errorStatutus.prettyPrint(), errorIndex and varBinds[int(errorIndex)-1][0] or '?'))
           break
       else:
           for varBind  in varBinds:
               print (' = '.join([x.prettyPrint() for x in varBind]))


hosts = [ ('192.168.10.150', 'AMC-9_1_DNCC_1'), ('192.168.10.151', 'AMC-9_1_DNCC_2'), 
    ('192.168.11.102', 'AMC-1_DNCC_1A'), ('192.168.11.49', 'AMC-1_DNCC_1A2'), 
    ('192.168.11.50', 'AMC-1_DNCC_1B'), ('192.168.11.103', 'AMC-1_DNCC_1B2')]

oids = [ {'.1.3.6.1.4.1.303.3.3.12.19.3.501.1.8': 'dnccQosInrouteNumUser'},
        {'.1.3.6.1.4.1.303.3.3.12.19.3.501.1.28': 'dnccQosInrouteIGPID'},
        {'.1.3.5..4.1.303.3.3.12.19.3.502.1.5': 'dnccQosRemThru'},
        {'.1.3.6.1.4.1.303.3.3.12.19.3.502.1.7': 'dnccQosRemTotalBacklog'} ]

for (ip, name) in hosts:
    print "Looking at IP address %s" % (ip, )
    goless.go(getSNMP(ip, oids))
Esempio n. 36
0
 def test_starts_with_params(self):
     called = mock.Mock()
     goless.go(called, 10, a=1)
     be.yield_()
     called.assert_called_once_with(10, a=1)
Esempio n. 37
0
def start_touch_sensor(brick, port, channel):
    print("start touch sensor")
    setup_sensor(brick, port)
    goless.go(run_touch_sensor, brick, port, channel)
    print("touch sensor started")
Esempio n. 38
0
 def test_starts_stuff(self):
     items = []
     goless.go(lambda: items.append(1))
     be.yield_()
     self.assertEqual(items, [1])
Esempio n. 39
0
 def test_starts_stuff(self):
     items = []
     goless.go(lambda: items.append(1))
     be.yield_()
     self.assertEqual(items, [1])
Esempio n. 40
0
    for _ in range(0, settings.iterations * settings.requests):
        population = migrated.recv()
        if settings.only_population:
            print(population['population'])
        else:
            print(population)


def log_to_redis_worker(settings):
    """ Logs to redis. """

    for _ in range(0, settings.iterations * settings.requests):
        population = redis_logs.recv()
        if settings.log:
            if settings.only_population:
                log_to_redis_population(population)
            else:
                log_to_redis_coco(population)


if __name__ == "__main__":
    # Gets the settings and makes the requests according to them using channels.

    settings = get_args()
    go(create_population_worker, settings)
    go(evolution_id_worker, settings)
    go(evolved_worker, settings)
    go(migrate_worker, settings)
    go(log_to_redis_worker, settings)
    print_worker(settings)
Esempio n. 41
0
def start_infrared_sensor(brick, port, channel):
    print("start infrared sensor")
    setup_sensor(brick, port)
    goless.go(run_infrared_sensor, brick, port, channel)
    print("infrared sensor started")
Esempio n. 42
0
def start_color_sensor(brick, port, channel):
    print("start color sensor")
    setup_sensor(brick, port)
    goless.go(run_color_sensor, brick, port, channel)
    print("color sensor started")