Skip to content

How to using with Django? #46

Open
@ghost

Description

How to include nats with Django application?

I'm trying run in from wsgi

def get_wsgi_application():
    django.setup(set_prefix=False)
    run_messaging()
    return WSGIHandler()

def run_messaging():
    def start_loop(loop):
        asyncio.set_event_loop(loop)
        loop.run_until_complete(run(loop))
        loop.run_forever()
    new_loop = asyncio.new_event_loop()
    t = Thread(target=start_loop, args=(new_loop,))
    t.start()


async def run(loop):
    async def vcm_calendar_create(msg):
        print("[{}] Received a message on queue subscription: {}".format(msg.sequence, msg.data))

    nc = NATS()
    sc = STAN()
    client_id = f'calendar-sub-{os.getpid()}'
    await nc.connect(settings.FDLT_NATS_URL, token=settings.FDLT_NATS_TOKEN, io_loop=loop)
    await sc.connect(settings.FDLT_NATS_STREAM_CLUSTER_ID, client_id, nats=nc)
    await sc.subscribe(subject="vcm", queue='vcm.calendar.create', cb=vcm_calendar_create)

But handler vcm_calendar_create can't work with Django ORM

Can you write example how implement it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions