This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import logging | |
from abc import ABCMeta, abstractmethod | |
from datetime import datetime, timedelta | |
from functools import partial | |
from typing import Optional, Any | |
from uuid import uuid4 | |
from dateutil import tz | |
from ntech.asyncio_utils import async_task |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Создание класса: | |
1a. Meta.__prepare__(mcs=<class '__main__.Meta'>, name='Class', bases=(), **{'extra': 1}) | |
Возвращает dictionary-like обьект, который будет использован для создания класса. Можно добавить что-то туда сразу (также доступны параметры из обьявления будущего класса в kwargs). | |
1b. "Проход" по методам Class, все добавляется в dictionary-like обьект. | |
2. Meta.__new__(mcs=<class '__main__.Meta'>, name='Class', bases=(), attrs=[added, __module__, __qualname__, __new__, __init__, __str__, __classcell__], **{'extra': 1}) | |
Вызывается с заполненным словарем будущего класса в attrs. Kwargs теже, что и в __prepare__. type создает новый класс. | |
3. Meta.__init__(cls=<class '__main__.Class'>, name='Class', bases=(), attrs=[added, __module__, __qualname__, __new__, __init__, __str__, __classcell__], **{'extra': 1}) | |
Инициализируется только что созданный класс. | |
Создание экземпляра: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import weakref | |
import functools | |
class AsyncCoroQueueDispatcher: | |
""" | |
Dispatcher gets coroutine from its iternal queue, | |
runs is asynchronously (in background) and waits untill it's done, | |
then gets next one. It's meant to be embed in another class. | |
Dispatcher can be easily rewritten for asyncio.PriorityQueue if needed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/Library/Frameworks/Python.framework/Versions/3.4/bin/python3 | |
""" | |
Synchronously call an asyncio task/coroutine from a non-asyncio thread. | |
usage: | |
# block the current thread until the coroutine yields or returns | |
value = sync_call(loop, coroutine, 1, 2, 3, four=4, five=5) | |
# control returns here when the coroutine is done | |
""" | |
import asyncio |