예제 #1
0
async def main():  # (defined as coroutine as it uses await)
  ch = Chan()  # Create a new channel.
  go(generate, ch)  # Start generate() as a coroutine.
  while True:
    try:  # (could also be written like above, with async for)
      prime = await ch.recv()
    except ChannelClosed:
      break
    print("{} ".format(prime), end='', flush=True)
    ch1 = Chan()
    go(filter, ch, ch1, prime)
    ch = ch1
예제 #2
0
        print(t, i)
    print("consume_iter DONE")


async def consume(t, c):
    while True:
        try:
            x = await c.recv()
        except:
            break
        print(t, x)
    print("consume DONE")


c = Chan()
go(give, 1, 21, c)
go(consume, "A", c)
go(consume_iter, "B", c)
go(consume, "C", c)


async def selectexample():
    s = Chan()
    d = Chan(1)
    cases = [('rec', s.recv()), ('rec', s.recv()), ('snd', s.send(3)),
             (d, d.send(1))]
    while cases:
        (id_, r), cases = await select(cases)
        if id_ == 'rec':
            print("received", r)
            if r == 3:
예제 #3
0
#!/usr/bin/env python3.5

from awaitchannel import Chan, select, go, ChannelClosed

c = Chan()
go(c.send, 4)  # start the send itself as coroutine

f = go(c.recv)  # start the receive as coroutine
x = f.result()
print("got", x)
예제 #4
0
파일: misc.py 프로젝트: pothos/awaitchannel
async def consume_iter(t, c):
  async for i in c:
    print(t, i)
  return

async def consume(t, c):
  while True:
    try:
      x = await c.recv()
    except:
      break
    print(t, x)

c = Chan()
go(give, 1, 21, c)
go(consume, "A", c)
go(consume_iter, "B", c)
go(consume, "C", c)

async def selectexample():
  s = Chan()
  d = Chan(1)
  cases = [('r', s.recv()), ('r', s.recv()), ('s', s.send(3)), (d, d.send(1))]
  while cases:
    (id_, r), cases = await select(cases)
    if id_ == 'r':
      print("received", r)
      if r == 3:
        cases.append(('s', s.send(5)))
    elif id_ == 's':
예제 #5
0
#!/usr/bin/env python3.5

from awaitchannel import Chan, select, go, ChannelClosed


c = Chan()
go(c.send, 4)

x = go(c.recv).result()
print(x)

예제 #6
0
async def generate(ch):
  # Send the sequence 2, 3, 4, ... to channel ch.
  for i in range(2, 300):
    await ch.send(i)  # Send i to channel ch.
  await ch.close()

async def filter(in_c, out_c, prime):
  # Copy the values from channel in to channel out,
  # removing those divisible by prime.
  async for i in in_c:  # (will stop on ChannelClosed)
    # Receive value of new variable i from in.
    if i % prime != 0:
      await out_c.send(i)  #  Send i to channel out.
  await out_c.close()

# The prime sieve: Daisy-chain filter processes together.
async def main():  # (defined as coroutine as it uses await)
  ch = Chan()  # Create a new channel.
  go(generate, ch)  # Start generate() as a coroutine.
  while True:
    try:  # (could also be written like above, with async for)
      prime = await ch.recv()
    except ChannelClosed:
      break
    print("{} ".format(prime), end='', flush=True)
    ch1 = Chan()
    go(filter, ch, ch1, prime)
    ch = ch1
go(main)
예제 #7
0
import time, random
from awaitchannel import Chan, select, go, ChannelClosed, loop

def blocking_sleep():
  i = random.randint(1, 3)
  print("sleeping for", i, "seconds")
  time.sleep(i)
  return i

async def count_sleeping():
  i = 0
  for _ in range(3):
    i += await loop.run_in_executor(None, blocking_sleep)
  print("slept", i, "seconds in total")

async def count_concurrent_sleeping():
  futures = []
  for _ in range(3):  # spawn in parallel
    futures.append(loop.run_in_executor(None, blocking_sleep))  # without await on the future
  s = 0
  for f in futures:
    s += await f
  print("slept concurrently", s, "seconds in total")

print("wait for each blocking function to finish before the next one starts")
go(count_sleeping).result()  # usage of result() forces to wait for count_sleeping to finish
print()
print("wait for each blocking function at the end")
go(count_concurrent_sleeping)
예제 #8
0
c = Chan()  # synchronous communication


async def give(v):
    for i in range(0, 10):
        await c.send(v + str(i))
    print('give', v, 'done')


async def consume(ID):
    async for v in c:
        print(ID, 'got', v)
        await loop.run_in_executor(
            None, time.sleep,
            0.1)  # sleep because python does not guarantee fairness
    print('consume', ID, 'done')


go(consume, 'A')
go(consume, 'B')
r1 = go(give, 'x')
go(consume, 'C')
go(consume, 'D')
r2 = go(give, 'y')
r3 = go(give, 'z')
r1.result()  # wait for sending to be complete
r2.result()
r3.result()
go(c.close)  # cleanup pending consume iters of 'async for' (i.e. await c.recv)
print("closed")