python协程面试题(一)

题目

使用协程的概念,达到以下目的, 输入a,b,c,d四个整数,打印(a+b)*(c+d)的值。假设a+b的过程是耗时1秒IO操作。

笔者的解答

"""
使用协程的概念,达到以下目的, 输入a,b,c,d四个整数,打印(a+b)*(c+d)的值
"""
import asyncio, os
from threading import current_thread


# 定义负责计算两个数字的和的协程
async def sum(a, b):
    print("【%s-%s】coroutine start to do: %s + %s" % (os.getpid(), current_thread().getName(), a, b))
    await asyncio.sleep(1) # 模拟耗时1秒的IO操作,自动切换协程
    r = int(a) + int(b)
    print("【%s-%s】coroutine end for : %s + %s,  result is %s" % (os.getpid(), current_thread().getName(), a, b, r))
    return r


# 定义主函数
def main(a, b, c, d):
    loop = asyncio.get_event_loop()
    task = asyncio.gather(
        sum(a, b),
        sum(c, d)
    )
    loop.run_until_complete(task)
    r1, r2 = task.result()
    r = r1 * r2
    print("【%s-%s】%s * %s = %s" % (os.getpid(), current_thread().getName(), r1, r2, r))
    loop.close()


if __name__ == '__main__':
    main(1, 2, 3, 4)

打印过程和结果:

【23729-MainThread】coroutine start to do: 1 + 223729-MainThread】coroutine start to do: 3 + 423729-MainThread】coroutine end for : 1 + 2,  result is 323729-MainThread】coroutine end for : 3 + 4,  result is 723729-MainThread】3 * 7 = 21

猜你喜欢

转载自blog.csdn.net/leon_wzm/article/details/79085235