summaryrefslogtreecommitdiff
path: root/concurr/basics/tutorials.py
diff options
context:
space:
mode:
authorchzhang <zch921005@126.com>2022-11-05 19:36:49 +0800
committerchzhang <zch921005@126.com>2022-11-05 19:36:49 +0800
commit24da1a1db479f26d2b854102d817baf4fa412b6e (patch)
tree9980388a299290c5b976599515ab27320051da55 /concurr/basics/tutorials.py
parent1b105470efecf90fe1a7e153191835cf0c4dfb19 (diff)
stable diffusion 01
Diffstat (limited to 'concurr/basics/tutorials.py')
-rw-r--r--concurr/basics/tutorials.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/concurr/basics/tutorials.py b/concurr/basics/tutorials.py
new file mode 100644
index 0000000..07fb056
--- /dev/null
+++ b/concurr/basics/tutorials.py
@@ -0,0 +1,48 @@
+
+import asyncio
+import time
+
+
+# define a coroutine
+# async def main():
+# print('hello coroutine!')
+#
+#
+# # pause the execution of a coroutine
+# async def main2():
+# await main()
+# print('continue execution')
+
+
+async def display_time():
+ start_time = time.time()
+ while True:
+ dur = int(time.time() - start_time)
+ if dur % 3 == 0:
+ print('{} seconds have passed.'.format(dur))
+ await asyncio.sleep(1)
+
+
+async def print_num():
+ num = 1
+ while True:
+ print(num)
+ num += 1
+ await asyncio.sleep(0.1)
+
+
+async def main():
+
+ task1 = asyncio.ensure_future(display_time())
+ task2 = asyncio.ensure_future(print_num())
+
+ await asyncio.gather(task1, task2)
+
+if __name__ == '__main__':
+
+ # asyncio.run(main())
+ ev_loop = asyncio.get_event_loop()
+
+ ev_loop.run_until_complete(main())
+
+ ev_loop.close()