2015-08-10 13:57:43 +00:00
|
|
|
# Copyright 2015 Canonical, Ltd.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
""" Async Handler
|
|
|
|
Provides async operations for various api calls and other non-blocking
|
|
|
|
work.
|
|
|
|
|
2015-08-23 22:48:52 +00:00
|
|
|
The way this works is you create your non-blocking thread:
|
2015-08-10 13:57:43 +00:00
|
|
|
|
|
|
|
.. code::
|
|
|
|
|
|
|
|
def my_async_method(self):
|
|
|
|
pool.submit(func, *args)
|
|
|
|
|
2015-08-23 22:48:52 +00:00
|
|
|
# In your controller you would then call using coroutines
|
2015-08-10 13:57:43 +00:00
|
|
|
|
2015-08-23 22:48:52 +00:00
|
|
|
@coroutine
|
|
|
|
def do_async_action(self):
|
2015-08-10 13:57:43 +00:00
|
|
|
try:
|
2015-08-23 22:48:52 +00:00
|
|
|
yield my_async_method()
|
|
|
|
# Move to next coroutine or return if finish
|
|
|
|
self.do_second_async_action()
|
|
|
|
except Exeception as e:
|
|
|
|
log.exception("Failed in our non-blocking code.")
|
2015-08-10 13:57:43 +00:00
|
|
|
"""
|
|
|
|
|
2015-09-11 18:12:26 +00:00
|
|
|
import logging
|
2015-08-23 22:48:52 +00:00
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
2016-06-30 18:17:01 +00:00
|
|
|
log = logging.getLogger("subiquitycore.async")
|
2015-08-10 13:57:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Async:
|
2015-09-11 18:12:26 +00:00
|
|
|
log.debug('instantiating ThreadPoolExector')
|
2015-09-11 20:53:34 +00:00
|
|
|
pool = ThreadPoolExecutor(1)
|
2015-09-11 18:12:26 +00:00
|
|
|
log.debug('pool={}'.format(pool))
|