Skip to content

no running event loop #18

@nodermann2

Description

@nodermann2

I can't define connection and start functions without running the event loop.

if PY_37:
    __get_running_loop = asyncio.get_running_loop  # by the reason you can't use the module without a running event loop

I want the connection variable to be global and access functions from other modules while the event loop is not running yet
conn = asynctnt.Connection(host='127.0.0.1', port=3301)

At the moment I have to make a crutch out of the initial function:

async def init_conn():
    global conn
    conn = asynctnt.Connection(host='127.0.0.1', port=3301)
    await asyncio.sleep(0)

so that I can access functions from other modules.

the full example which doesn't work without a crutch:

import asyncio
import asynctnt

conn = asynctnt.Connection(host='127.0.0.1', port=3301)

async def insert(space: str, data, replace=False):
    if not conn.is_connected:
        await conn.connect()
    result = await conn.insert(space, list(data), replace=replace)
    return result[0] if len(result) == 1 else result


async def update(space: str, key, ops):
    if not conn.is_connected:
        await conn.connect()
    key = [key] if type(key) is not list else key
    result = await conn.update(space, key, ops)
    return result[0] if len(result) > 0 else None


async def select(space: str, key=None, index='primary', unpack=True):
    if not conn.is_connected:
        await conn.connect()
    key = [key] if type(key) is not list and key else key
    result = await conn.select(space, key=key, index=index)
    return result[0] if len(result) == 1 and unpack else result


async def sql(query, args=None, parse_metadata=False):
    if not conn.is_connected:
        await conn.connect()
    result = await conn.sql(query, args, parse_metadata=parse_metadata)
    return result


async def delete(space: str, key):
    if not conn.is_connected:
        await conn.connect()
    key = [key] if type(key) is not list else key
    await conn.delete(space, key)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions