Switch to Tornado's event loop
This provides us with several additional features the builtin select() loop does not, such as: - Chainable async actions via coroutines - asyncio bridge This will keep the code simpler and read more like a synchronous application. Signed-off-by: Adam Stokes <adam.stokes@ubuntu.com>
This commit is contained in:
parent
d8a401447f
commit
0b7f217339
|
@ -5,3 +5,4 @@ nose-cov
|
|||
nose
|
||||
flake8
|
||||
python3-parted
|
||||
tornado
|
||||
|
|
|
@ -17,30 +17,27 @@
|
|||
Provides async operations for various api calls and other non-blocking
|
||||
work.
|
||||
|
||||
The way this works is you create your IO/CPU bound thread:
|
||||
The way this works is you create your non-blocking thread:
|
||||
|
||||
.. code::
|
||||
|
||||
def my_async_method(self):
|
||||
pool.submit(func, *args)
|
||||
|
||||
# In your controller you would then call
|
||||
# In your controller you would then call using coroutines
|
||||
|
||||
my_async_method_f = my_async_method()
|
||||
my_async_method_f.add_done_callback(self.handle_async_method)
|
||||
|
||||
def handle_async_method(self, future):
|
||||
@coroutine
|
||||
def do_async_action(self):
|
||||
try:
|
||||
result = future.result()
|
||||
except Exception as e:
|
||||
raise Exception("Program in thread {}".format(e))
|
||||
|
||||
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.")
|
||||
"""
|
||||
|
||||
from multiprocessing import cpu_count
|
||||
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
|
||||
class Async:
|
||||
pool = ThreadPoolExecutor(10)
|
||||
ppool = ProcessPoolExecutor(cpu_count())
|
||||
pool = ThreadPoolExecutor(4)
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
import logging
|
||||
import urwid
|
||||
import urwid.curses_display
|
||||
from tornado.ioloop import IOLoop
|
||||
from subiquity.signals import Signal
|
||||
from subiquity.palette import STYLES, STYLES_MONO
|
||||
|
||||
|
@ -102,8 +103,10 @@ class Controller:
|
|||
additional_opts['screen'].set_terminal_properties(colors=256)
|
||||
additional_opts['screen'].reset_default_terminal_palette()
|
||||
|
||||
evl = urwid.TornadoEventLoop(IOLoop())
|
||||
self.loop = urwid.MainLoop(
|
||||
self.ui, palette, **additional_opts)
|
||||
self.ui, palette, event_loop=evl, **additional_opts)
|
||||
log.debug("Running event loop: {}".format(self.loop.event_loop))
|
||||
|
||||
try:
|
||||
self.set_alarm_in(0.05, self.welcome)
|
||||
|
|
Loading…
Reference in New Issue