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@AsyncRunner.run_in_task() 30async def func1(x, y): 31 print('FUNC1: start', x, y) 32 await asyncio.sleep(x) 33 print('FUNC1: end', x, y) 34 35 36# ----------------------------------------------------------------------------- 37@AsyncRunner.run_in_task(queue=my_work_queue1) 38async def func2(x, y): 39 print('FUNC2: start', x, y) 40 await asyncio.sleep(x) 41 print('FUNC2: end', x, y) 42 43 44# ----------------------------------------------------------------------------- 45@AsyncRunner.run_in_task(queue=my_work_queue2) 46async def func3(x, y): 47 print('FUNC3: start', x, y) 48 await asyncio.sleep(x) 49 print('FUNC3: end', x, y) 50 51 52# ----------------------------------------------------------------------------- 53@AsyncRunner.run_in_task(queue=None) 54async def func4(x, y): 55 print('FUNC4: start', x, y) 56 await asyncio.sleep(x) 57 print('FUNC4: end', x, y) 58 59 raise ValueError('test') 60 61 62# ----------------------------------------------------------------------------- 63async def main(): 64 print("MAIN: start, loop=", asyncio.get_running_loop()) 65 print("MAIN: invoke func1") 66 func1(1, 2) 67 68 print("MAIN: invoke func2") 69 func2(3, 4) 70 71 print("MAIN: invoke func3") 72 func3(5, 6) 73 74 print("MAIN: invoke func4") 75 func4(7, 8) 76 77 print("MAIN: sleeping 2 seconds") 78 await asyncio.sleep(2) 79 print("MAIN: running my_work_queue2.run") 80 await my_work_queue2.run() 81 print("MAIN: end (should never get here)") 82 83 84# ----------------------------------------------------------------------------- 85logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper()) 86asyncio.run(main()) 87