示例#1
0
文件: ex3.py 项目: xyicheng/thredo
def hello(sec):
    print("Yawn")
    try:
        thredo.sleep(sec)
        print("Awake")
    except thredo.ThreadCancelled:
        print("Cancelled!")
示例#2
0
文件: ex3.py 项目: xyicheng/thredo
def func():
    print('Yawn')
    try:
        thredo.sleep(10)
        print('Awake')
    except thredo.ThreadCancelled:
        print('Cancelled')
示例#3
0
文件: prod.py 项目: xyicheng/thredo
def consumer(q):
    while True:
        item = q.get()
        if item is None:
            break
        print("Child:", item)
        thredo.sleep(0.01)
示例#4
0
 def main():
     serv = thredo.spawn(server, ('', 25000))
     client = thredo.spawn(test_client, ('localhost', 25000))
     thredo.sleep(0.1)
     client.cancel()
     start.set()
     serv.join()
示例#5
0
文件: ex4.py 项目: xyicheng/thredo
def main():
    lck = thredo.Semaphore()
    t1 = thredo.spawn(func, lck, 'thread-1')
    t2 = thredo.spawn(func, lck, 'thread-2')
    # Case 2: Cancel a thread holding a lock
    thredo.sleep(2)
    t1.cancel()
    t2.join()
示例#6
0
 def producer():
     nonlocal n
     while n < 5:
         thredo.sleep(0.1)
         with lock:
             n += 1
             result.append(('produce', n))
             lock.notify()
示例#7
0
def friend(name):
    print('Hi, my name is', name)
    print('Playing Minecraft')
    try:
        thredo.sleep(1000)
    except thredo.CancelledError:
        print(name, 'going home')
        raise
示例#8
0
 def test_client(address):
     evt.wait()
     sock = socket(AF_INET, SOCK_STREAM)
     sock.connect(address)
     sock.recv(8)
     sock.send(b'Msg1\nMsg2\n')
     thredo.sleep(1)
     sock.send(b'Msg3\n')
     sock.close()
示例#9
0
文件: ex6.py 项目: xyicheng/thredo
def main():
#    thredo.spawn(spin, 60_000_000)
    q = thredo.Queue()
    with thredo.ThreadGroup(wait=None) as g:
        g.spawn(spin,100_000_000)
        for n in range(4):
            g.spawn(worker, q, f'Worker-{n}')
            
        for n in range(10):
            q.put(n)
            thredo.sleep(1)
            
        q.join()
        
    print('Done')
示例#10
0
 def server(address):
     sock = socket(AF_INET, SOCK_STREAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(address)
     sock.listen(1)
     evt.set()
     client, addr = sock.accept()
     try:
         client.send(b'OK')
         s = client.as_stream()
         thredo.sleep(1)
         results.append(s.readall())
     finally:
         client.close()
         sock.close()
示例#11
0
def kid():
    while True:
        try:
            print('Can I play?')
            thredo.timeout_after(1, start_evt.wait)
            break
        except thredo.ThreadTimeout:
            print('Wha!?!')

    print('Building the Millenium Falcon in Minecraft')
    with thredo.ThreadGroup() as f:
        f.spawn(friend, 'Max')
        f.spawn(friend, 'Lillian')
        f.spawn(friend, 'Thomas')
        try:
            thredo.sleep(1000)
        except thredo.CancelledError:
            print('Fine. Saving my work.')
            raise
示例#12
0
def parent():
    goodbye = thredo.SignalEvent(signal.SIGINT, signal.SIGTERM)

    kid_task = thredo.spawn(kid)
    thredo.sleep(5)
    print("Yes, go play")
    start_evt.set()

    goodbye.wait()
    del goodbye

    print("Let's go")
    count_task = thredo.spawn(countdown, 10)
    count_task.join()

    print("We're leaving!")
    try:
        thredo.timeout_after(10, kid_task.join)
    except thredo.ThreadTimeout:
        kid_task.cancel()
        thredo.sleep(3)
    print('Parent Leaving')
示例#13
0
def philosopher(n):
    thredo.sleep(random.random())
    try:
        with sticks[n]:
            thredo.sleep(random.random())
            with sticks[(n + 1) % 5]:
                print("eating", n)
                thredo.sleep(random.random())
    except thredo.ThreadCancelled:
        print(f"But what is death? {n}")
示例#14
0
 def main():
     with thredo.ignore_after(0.25):
         thredo.sleep(1)
示例#15
0
def countdown(n):
    while n > 0:
        print('T-minus', n)
        thredo.sleep(1)
        n -= 1
示例#16
0
 def child():
     try:
         thredo.sleep(10)
     except thredo.ThreadCancelled:
         result.append('cancel')
示例#17
0
 def main():
     t = thredo.spawn(child)
     thredo.sleep(0.1)
     t.cancel()
示例#18
0
 def main():
     try:
         with thredo.timeout_after(0.25):
             thredo.sleep(1)
     except thredo.ThreadTimeout:
         result.append('timeout')
示例#19
0
文件: ex3.py 项目: xyicheng/thredo
def main():
    t = thredo.spawn(hello, 100)
    thredo.sleep(5)
    t.cancel()
    print("Goodbye")
示例#20
0
 def main():
     q = thredo.Queue(maxsize=1)
     t = thredo.spawn(producer, q)
     results.append('start')
     thredo.sleep(0.1)
     t.cancel()
示例#21
0
文件: echo2.py 项目: xyicheng/thredo
def killer(delay, t):
    thredo.sleep(delay)
    t.cancel()
示例#22
0
文件: ex4.py 项目: xyicheng/thredo
def func(lck, label):
    print(label, 'starting')
    with lck:
        print(label, 'working')
        thredo.sleep(5)
        print(label, 'done')
示例#23
0
文件: ex3.py 项目: xyicheng/thredo
def main():
    t = thredo.spawn(func)
    thredo.sleep(2)
    t.cancel()
示例#24
0
 def main():
     start = time.time()
     thredo.sleep(1.0)
     end = time.time()
     result.append(end - start)