1# Copyright 2021-2022 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15# ----------------------------------------------------------------------------- 16# Imports 17# ----------------------------------------------------------------------------- 18import logging 19import asyncio 20import os 21 22from bumble.utils import AsyncRunner 23 24# ----------------------------------------------------------------------------- 25my_work_queue1 = AsyncRunner.WorkQueue() 26my_work_queue2 = AsyncRunner.WorkQueue(create_task=False) 27 28 29# ----------------------------------------------------------------------------- 30@AsyncRunner.run_in_task() 31async def func1(x, y): 32 print('FUNC1: start', x, y) 33 await asyncio.sleep(x) 34 print('FUNC1: end', x, y) 35 36 37# ----------------------------------------------------------------------------- 38@AsyncRunner.run_in_task(queue=my_work_queue1) 39async def func2(x, y): 40 print('FUNC2: start', x, y) 41 await asyncio.sleep(x) 42 print('FUNC2: end', x, y) 43 44 45# ----------------------------------------------------------------------------- 46@AsyncRunner.run_in_task(queue=my_work_queue2) 47async def func3(x, y): 48 print('FUNC3: start', x, y) 49 await asyncio.sleep(x) 50 print('FUNC3: end', x, y) 51 52 53# ----------------------------------------------------------------------------- 54@AsyncRunner.run_in_task(queue=None) 55async def func4(x, y): 56 print('FUNC4: start', x, y) 57 await asyncio.sleep(x) 58 print('FUNC4: end', x, y) 59 60 raise ValueError('test') 61 62 63# ----------------------------------------------------------------------------- 64async def main() -> None: 65 print("MAIN: start, loop=", asyncio.get_running_loop()) 66 print("MAIN: invoke func1") 67 func1(1, 2) 68 69 print("MAIN: invoke func2") 70 func2(3, 4) 71 72 print("MAIN: invoke func3") 73 func3(5, 6) 74 75 print("MAIN: invoke func4") 76 func4(7, 8) 77 78 print("MAIN: sleeping 2 seconds") 79 await asyncio.sleep(2) 80 print("MAIN: running my_work_queue2.run") 81 await my_work_queue2.run() 82 print("MAIN: end (should never get here)") 83 84 85# ----------------------------------------------------------------------------- 86logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper()) 87asyncio.run(main()) 88