Skip to content

Commit 9ab5865

Browse files
authored
Merge pull request #1121 from minrk/async
async fixes
2 parents 0fa9594 + 69c52fb commit 9ab5865

3 files changed

Lines changed: 15 additions & 9 deletions

File tree

nbviewer/app.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Distributed under the terms of the BSD License. The full license is in
55
# the file COPYING, distributed as part of this software.
66
# -----------------------------------------------------------------------------
7+
import asyncio
78
import io
89
import json
910
import logging
@@ -21,7 +22,6 @@
2122
from nbconvert import get_exporter # type: ignore
2223
from nbconvert.exporters.templateexporter import ExtensionTolerantLoader # type: ignore
2324
from tornado import httpserver
24-
from tornado import ioloop
2525
from tornado import web
2626
from tornado.curl_httpclient import curl_log
2727
from tornado.log import access_log
@@ -802,6 +802,17 @@ def __init__(self, *args, **kwargs):
802802

803803
def main(argv=None):
804804
# create and start the app
805+
loop = asyncio.new_event_loop()
806+
loop.run_until_complete(async_main(argv=argv))
807+
try:
808+
loop.run_forever()
809+
except KeyboardInterrupt:
810+
pass
811+
finally:
812+
loop.close()
813+
814+
815+
async def async_main(argv=None):
805816
nbviewer = NBViewer()
806817
app = nbviewer.tornado_application
807818

@@ -819,10 +830,6 @@ def main(argv=None):
819830
)
820831

821832
http_server.listen(nbviewer.port, nbviewer.host)
822-
try:
823-
ioloop.IOLoop.current().start()
824-
except KeyboardInterrupt:
825-
pass
826833

827834

828835
if __name__ == "__main__":

nbviewer/cache.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ def __init__(self, *args, **kwargs):
124124
self.mc = pylibmc.Client(*args, **kwargs)
125125
self.mc_pool = pylibmc.ThreadMappedPool(self.mc)
126126

127-
self.loop = asyncio.get_event_loop()
128-
129127
async def _call_in_thread(self, method_name, *args, **kwargs):
130128
# https://stackoverflow.com/questions/34376814/await-future-from-executor-future-cant-be-used-in-await-expression
131129

@@ -140,7 +138,8 @@ def f():
140138
meth = getattr(mc, method_name)
141139
return meth(*args, **kwargs)
142140

143-
return await self.loop.run_in_executor(self.pool, f)
141+
loop = asyncio.get_running_loop()
142+
return await loop.run_in_executor(self.pool, f)
144143

145144
async def get(self, *args, **kwargs):
146145
return await self._call_in_thread("get", *args, **kwargs)

nbviewer/providers/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ async def finish_notebook(
675675
"Rendering %d B notebook from %s", len(json_notebook), download_url
676676
)
677677
render_time = self.statsd.timer("rendering.nbrender.time").start()
678-
loop = asyncio.get_event_loop()
678+
loop = asyncio.get_running_loop()
679679
nbhtml, config = await loop.run_in_executor(
680680
self.pool,
681681
render_notebook,

0 commit comments

Comments
 (0)