Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 46 additions & 43 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,61 @@
# -*- coding: utf-8 -*-
import sys
from contextlib import suppress

from sphinx_celery import conf

sys.path.append('.')
sys.path.append(".")

extensions = []

globals().update(conf.build_config(
'mode', __file__,
project='Mode',
# version_dev='2.0',
# version_stable='1.4',
canonical_url='http://mode-streaming.readthedocs.io',
webdomain='',
github_project='faust-streaming/mode',
copyright='2017-2020',
html_logo='images/logo.png',
html_favicon='images/favicon.ico',
html_prepend_sidebars=[],
include_intersphinx={'python', 'sphinx'},
extra_extensions=[
'sphinx.ext.napoleon',
'sphinx_autodoc_annotation',
'alabaster',
],
extra_intersphinx_mapping={
},
# django_settings='testproj.settings',
# from pathlib import Path
# path_additions=[Path.cwd().parent / 'testproj']
apicheck_ignore_modules=[
'mode.loop.eventlet',
'mode.loop.gevent',
'mode.loop.uvloop',
'mode.loop._gevent_loop',
'mode.utils',
'mode.utils._py37_contextlib',
'mode.utils.graphs.formatter',
'mode.utils.graphs.graph',
'mode.utils.types',
],
))

html_theme = 'alabaster'
globals().update(
conf.build_config(
"mode",
__file__,
project="Mode",
# version_dev='2.0',
# version_stable='1.4',
canonical_url="http://mode-streaming.readthedocs.io",
webdomain="",
github_project="faust-streaming/mode",
copyright="2017-2020",
html_logo="images/logo.png",
html_favicon="images/favicon.ico",
html_prepend_sidebars=[],
include_intersphinx={"python", "sphinx"},
extra_extensions=[
"sphinx.ext.napoleon",
"sphinx_autodoc_annotation",
"alabaster",
],
extra_intersphinx_mapping={},
# django_settings='testproj.settings',
# from pathlib import Path
# path_additions=[Path.cwd().parent / 'testproj']
apicheck_ignore_modules=[
"mode.loop.eventlet",
"mode.loop.gevent",
"mode.loop.uvloop",
"mode.loop._gevent_loop",
"mode.utils",
"mode.utils._py37_contextlib",
"mode.utils.graphs.formatter",
"mode.utils.graphs.graph",
"mode.utils.types",
],
)
)

html_theme = "alabaster"
html_sidebars = {}
templates_path = ['_templates']
templates_path = ["_templates"]

autodoc_member_order = 'bysource'
autodoc_member_order = "bysource"

pygments_style = 'sphinx'
pygments_style = "sphinx"

# This option is deprecated and raises an error.
with suppress(NameError):
del(html_use_smartypants) # noqa
del html_use_smartypants # noqa

extensions.remove('sphinx.ext.viewcode')
extensions.remove("sphinx.ext.viewcode")
11 changes: 5 additions & 6 deletions examples/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@


class MyService(mode.Service):

async def on_started(self) -> None:
self.log.info('Service started (hit ctrl+C to exit).')
self.log.info("Service started (hit ctrl+C to exit).")

@mode.Service.task
async def _background_task(self) -> None:
print('BACKGROUND TASK STARTING')
print("BACKGROUND TASK STARTING")
while not self.should_stop:
await self.sleep(1.0)
print('BACKGROUND SERVICE WAKING UP')
print("BACKGROUND SERVICE WAKING UP")


if __name__ == '__main__':
if __name__ == "__main__":
mode.Worker(
MyService(),
loglevel='INFO',
loglevel="INFO",
logfile=None, # stderr
# when daemon the worker must be explicitly stopped to end.
daemon=True,
Expand Down
55 changes: 27 additions & 28 deletions examples/tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any, List, MutableMapping

from aiohttp.web import Application

from mode import Service
from mode.threads import ServiceThread
from mode.utils.objects import cached_property
Expand All @@ -12,17 +13,16 @@ class User:


def remove_expired_users(d):
print('REMOVING EXPIRED USERS')
print("REMOVING EXPIRED USERS")
... # implement yourself


async def run_websocket_server():
print('STARTING WEBSOCKET SERVER')
print("STARTING WEBSOCKET SERVER")
... # implement yourself


class Websockets(Service):

def __init__(self, port: int = 8081, **kwargs: Any) -> None:
self.port = 8081
self._server = None
Expand All @@ -37,11 +37,7 @@ async def on_stop(self) -> None:


class Webserver(ServiceThread):

def __init__(self,
port: int = 8000,
bind: str = None,
**kwargs: Any) -> None:
def __init__(self, port: int = 8000, bind: str = None, **kwargs: Any) -> None:
self._app = Application()
self.port = port
self.bind = bind
Expand All @@ -53,28 +49,27 @@ async def on_start(self) -> None:
handler = self._handler = self._app.make_handler()
# self.loop is the event loop in this thread
# self.parent_loop is the loop that created this thread.
self._srv = await self.loop.create_server(
handler, self.bind, self.port)
self.log.info('Serving on port %s', self.port)
self._srv = await self.loop.create_server(handler, self.bind, self.port)
self.log.info("Serving on port %s", self.port)

async def on_thread_stop(self) -> None:
# on_thread_stop() executes in the thread.
# on_stop() executes in parent thread.

# quite a few steps required to stop the aiohttp server:
if self._srv is not None:
self.log.info('Closing server')
self.log.info("Closing server")
self._srv.close()
self.log.info('Waiting for server to close handle')
self.log.info("Waiting for server to close handle")
await self._srv.wait_closed()
if self._app is not None:
self.log.info('Shutting down web application')
self.log.info("Shutting down web application")
await self._app.shutdown()
if self._handler is not None:
self.log.info('Waiting for handler to shut down')
self.log.info("Waiting for handler to shut down")
await self._handler.shutdown(60.0)
if self._app is not None:
self.log.info('Cleanup')
self.log.info("Cleanup")
await self._app.cleanup()


Expand All @@ -97,12 +92,13 @@ async def _remove_expired(self):


class App(Service):

def __init__(self,
web_port: int = 8000,
web_bind: str = None,
websocket_port: int = 8001,
**kwargs: Any) -> None:
def __init__(
self,
web_port: int = 8000,
web_bind: str = None,
websocket_port: int = 8001,
**kwargs: Any
) -> None:
self.web_port = web_port
self.web_bind = web_bind
self.websocket_port = websocket_port
Expand All @@ -116,14 +112,16 @@ def on_init_dependencies(self) -> List:
]

async def on_start(self) -> None:
import pydot
import io

import pydot

o = io.StringIO()
beacon = self.beacon.root or self.beacon
beacon.as_graph().to_dot(o)
graph, = pydot.graph_from_dot_data(o.getvalue())
print('WRITING GRAPH TO image.png')
with open('image.png', 'wb') as fh:
(graph,) = pydot.graph_from_dot_data(o.getvalue())
print("WRITING GRAPH TO image.png")
with open("image.png", "wb") as fh:
fh.write(graph.create_png())

@cached_property
Expand All @@ -150,6 +148,7 @@ def user_cache(self) -> UserCache:

app = App()

if __name__ == '__main__':
if __name__ == "__main__":
from mode.worker import Worker
Worker(app, loglevel='info', daemon=True).execute_from_commandline()

Worker(app, loglevel="info", daemon=True).execute_from_commandline()
Loading