Skip to content

Instantly share code, notes, and snippets.

@megabuz
Last active May 12, 2016 09:10
Show Gist options
  • Save megabuz/e29d85b590d707d820c1eff256ceb305 to your computer and use it in GitHub Desktop.
Save megabuz/e29d85b590d707d820c1eff256ceb305 to your computer and use it in GitHub Desktop.
micro services

Конфигурация

  • python3.5
  • asyncio

Обработка урлов вида

  • /v1/get - get_v1 - сообщение в сентри об использовании устаревшего метода
  • /v2/get - get

Запуск сервера в синхронном режиме (один пользователь на процесс)

uwsgi --http :9090 --wsgi-file app.py

Запуск сервера в асинхронном режиме

python3.5 app.py --async

Запуск сервера в режиме консьюмера очереди

python3.5 app.py --consumer
# для инциалищации
from example import views # noqa
from cianlib import Runner
runner = Runner(version=2)
# для wsgi
application = runner.wsgi_application # noqa
runner.maybe_run() # asyncio, consumer run
from cian.entities import Entity, attrs
class ExampleEntity(Entity):
id = attrs.UnsignedInteger(help='ID')
title = attrs.String(help='Название')
image_url = attrs.String(help='URL картинки')
object_count = attrs.Integer(help='Количество объектов')
from cianlib.schemas import fields, Schema, EntitySchema
from exmaple.entities import ExampleEntity
class RequestSchema(Schema):
user_id = fields.Integer(required=True, min=1, help='ID пользователя')
limit = fields.Integer(required=True, min=0, help='Лимит')
offset = fields.Integer(required=True, min=0, help='Оффсет')
class ResponseSchemaOld(EntitySchema):
class Meta:
entity = ExampleEntity
lowercase = True
from cianlib import api
from cianlib.schemas import EntitySchema
from example.schemas import RequestSchema, ResponseSchemaOld
from exmaple.entities import ExampleEntity
@api.get(RequestSchema, EntitySchema.by(ExampleEntity))
def get(params):
yield from asyncio.sleep(3)
return ExampleEntity(
id=3,
title='Название',
image_url='http://google.com/logo.png',
object_count=3
)
@api.get(RequestSchema, ResponseSchemaOld)
def get_v1(params):
yield from asyncio.sleep(3)
return ExampleEntity(
id=3,
image_url='http://google.com/logo.png',
object_count=3
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment