Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Copyright (C) 2018 Bloomberg LP 

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# <http://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 

16import asyncio 

17import logging 

18 

19 

20class Bot: 

21 """Creates a local BotSession.""" 

22 

23 def __init__(self, bot_session): 

24 self.__logger = logging.getLogger(__name__) 

25 

26 self.__bot_session = bot_session 

27 

28 self.__loop = None 

29 

30 def session(self): 

31 """Will create a session and periodically call the server.""" 

32 

33 self.__loop = asyncio.get_event_loop() 

34 

35 try: 

36 task = asyncio.ensure_future(self.__bot_session.run()) 

37 self.__loop.run_until_complete(task) 

38 except KeyboardInterrupt: 

39 pass 

40 

41 self.__kill_everyone() 

42 self.__logger.info("Bot shutdown.") 

43 

44 def __kill_everyone(self): 

45 """Cancels and waits for them to stop.""" 

46 self.__logger.info("Cancelling remaining tasks...") 

47 for task in asyncio.Task.all_tasks(): 

48 task.cancel() 

49 self.__loop.run_until_complete(task)