本文整理汇总了Python中asgiref.sync.async_to_sync方法的典型用法代码示例。如果您正苦于以下问题:Python sync.async_to_sync方法的具体用法?Python sync.async_to_sync怎么用?Python sync.async_to_sync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类asgiref.sync
的用法示例。
在下文中一共展示了sync.async_to_sync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _abort_processing
# 需要导入模块: from asgiref import sync [as 别名]
# 或者: from asgiref.sync import async_to_sync [as 别名]
def _abort_processing(self, obj):
"""Abort processing of the current data object.
Also notify worker and frontend.
"""
async_to_sync(self._send_reply)(
obj, {ExecutorProtocol.RESULT: ExecutorProtocol.RESULT_ERROR}
)
async_to_sync(consumer.send_event)(
{
WorkerProtocol.COMMAND: WorkerProtocol.ABORT,
WorkerProtocol.DATA_ID: obj[ExecutorProtocol.DATA_ID],
WorkerProtocol.FINISH_COMMUNICATE_EXTRA: {
"executor": getattr(settings, "FLOW_EXECUTOR", {}).get(
"NAME", "resolwe.flow.executors.local"
),
},
}
)
示例2: async_test
# 需要导入模块: from asgiref import sync [as 别名]
# 或者: from asgiref.sync import async_to_sync [as 别名]
def async_test(func):
"""
Wrap async_to_sync with another function because Pytest complains about
collecting the resulting callable object as a test because it's not a true
function:
PytestCollectionWarning: cannot collect 'test_foo' because it is not a
function.
"""
# inner import because for Python 3.6+ tests only
from asgiref.sync import async_to_sync
sync_func = async_to_sync(func)
@functools.wraps(func)
def wrapper(*args, **kwargs):
return sync_func(*args, **kwargs)
return wrapper
示例3: test_perform_both
# 需要导入模块: from asgiref import sync [as 别名]
# 或者: from asgiref.sync import async_to_sync [as 别名]
def test_perform_both(self):
service = NabWeatherd()
writer = MockWriter()
service.writer = writer
config_t = ("75005", NabWeatherd.UNIT_CELSIUS, "both")
expiration = datetime.datetime(2019, 4, 22, 0, 0, 0)
async_to_sync(service.perform)(expiration, "today", config_t)
self.assertEqual(len(writer.written), 3)
packet = writer.written[0]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "info")
self.assertEqual(packet_json["info_id"], "nabweatherd_rain")
packet = writer.written[1]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "info")
self.assertEqual(packet_json["info_id"], "nabweatherd")
self.assertTrue("animation" in packet_json)
packet = writer.written[2]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "message")
self.assertTrue("signature" in packet_json)
self.assertTrue("body" in packet_json)
示例4: test_perform_rain
# 需要导入模块: from asgiref import sync [as 别名]
# 或者: from asgiref.sync import async_to_sync [as 别名]
def test_perform_rain(self):
service = NabWeatherd()
writer = MockWriter()
service.writer = writer
config_t = ("75005", NabWeatherd.UNIT_CELSIUS, "rain")
expiration = datetime.datetime(2019, 4, 22, 0, 0, 0)
async_to_sync(service.perform)(expiration, "today", config_t)
self.assertEqual(len(writer.written), 3)
packet = writer.written[0]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "info")
self.assertEqual(packet_json["info_id"], "nabweatherd_rain")
packet = writer.written[1]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "info")
self.assertEqual(packet_json["info_id"], "nabweatherd")
self.assertFalse("animation" in packet_json)
packet = writer.written[2]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "message")
self.assertTrue("signature" in packet_json)
self.assertTrue("body" in packet_json)
示例5: test_perform
# 需要导入模块: from asgiref import sync [as 别名]
# 或者: from asgiref.sync import async_to_sync [as 别名]
def test_perform(self):
service = NabWeatherd()
writer = MockWriter()
service.writer = writer
config_t = ("75005", NabWeatherd.UNIT_CELSIUS, "weather")
expiration = datetime.datetime(2019, 4, 22, 0, 0, 0)
async_to_sync(service.perform)(expiration, "today", config_t)
self.assertEqual(len(writer.written), 3)
packet = writer.written[0]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "info")
self.assertEqual(packet_json["info_id"], "nabweatherd_rain")
packet = writer.written[1]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "info")
self.assertEqual(packet_json["info_id"], "nabweatherd")
self.assertTrue("animation" in packet_json)
packet = writer.written[2]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "message")
self.assertTrue("signature" in packet_json)
self.assertTrue("body" in packet_json)
示例6: test_perform
# 需要导入模块: from asgiref import sync [as 别名]
# 或者: from asgiref.sync import async_to_sync [as 别名]
def test_perform(self):
config = models.Config.load()
config.index_airquality = "aqi"
config.visual_airquality = "always"
config.localisation = None
config.save()
service = NabAirqualityd()
writer = MockWriter()
service.writer = writer
config_t = ("aqi", "always")
expiration = datetime.datetime(2019, 4, 22, 0, 0, 0)
async_to_sync(service.perform)(expiration, "today", config_t)
self.assertEqual(len(writer.written), 2)
packet = writer.written[0]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "info")
self.assertEqual(packet_json["info_id"], "nabairqualityd")
self.assertTrue("animation" in packet_json)
packet = writer.written[1]
packet_json = json.loads(packet.decode("utf8"))
self.assertEqual(packet_json["type"], "message")
self.assertTrue("signature" in packet_json)
self.assertTrue("body" in packet_json)
示例7: _queue_response_channel
# 需要导入模块: from asgiref import sync [as 别名]
# 或者: from asgiref.sync import async_to_sync [as 别名]
def _queue_response_channel(self, obj):
"""Generate the feedback channel name from the object's id.
:param obj: The Channels message object.
"""
return "{}.{}".format(
state.MANAGER_EXECUTOR_CHANNELS.queue_response,
obj[ExecutorProtocol.DATA_ID],
)
# The handle_* methods are all Django synchronized, meaning they're run
# in separate threads. Having this method be sync and calling async_to_sync
# on rpush itself would mean reading self._redis from the sync thread,
# which isn't very tidy. If it's async, it'll be called from the main
# thread by the async_to_sync calls in handle_*.
示例8: handle_get_referenced_files
# 需要导入模块: from asgiref import sync [as 别名]
# 或者: from asgiref.sync import async_to_sync [as 别名]
def handle_get_referenced_files(self, obj):
"""Get a list of files referenced by the data object.
To get the entire output this request must be sent after processing
is finished.
:param obj: The Channels message object. Command object format:
.. code-block:: none
{
'command': 'get_referenced_data',
'data_id': [id of the :class:`~resolwe.flow.models.Data`
object],
}
"""
try:
data_id = obj[ExecutorProtocol.DATA_ID]
data = Data.objects.get(pk=data_id)
except Data.DoesNotExist:
logger.error(
"Data object does not exist (handle_get_referenced_files).",
extra={"data_id": data_id,},
)
self._abort_processing(obj)
return
async_to_sync(self._send_reply)(
obj,
{
ExecutorProtocol.RESULT: ExecutorProtocol.RESULT_OK,
ExecutorProtocol.REFERENCED_FILES: referenced_files(data),
},
)
示例9: handle_get_files_to_download
# 需要导入模块: from asgiref import sync [as 别名]
# 或者: from asgiref.sync import async_to_sync [as 别名]
def handle_get_files_to_download(self, obj):
"""Get a list of files belonging to a given storage location object.
:param obj: The Channels message object. Command object format:
.. code-block:: none
{
'command': 'get_files_to_download',
'data_id': [id of the :class:`~resolwe.flow.models.Data`
object],
'storage_location_id': id of the :class:`~resolwe.storage.models.StorageLocation` object.
}
"""
try:
storage_location_id = obj[ExecutorProtocol.STORAGE_LOCATION_ID]
location = StorageLocation.objects.get(pk=storage_location_id)
except StorageLocation.DoesNotExist:
logger.error(
"StorageLocation object does not exist (handle_get_files_to_download).",
extra={"storage_location_id": storage_location_id,},
)
self._abort_processing(obj)
return
async_to_sync(self._send_reply)(
obj,
{
ExecutorProtocol.RESULT: ExecutorProtocol.RESULT_OK,
ExecutorProtocol.REFERENCED_FILES: list(location.files.values()),
},
)
示例10: handle_abort
# 需要导入模块: from asgiref import sync [as 别名]
# 或者: from asgiref.sync import async_to_sync [as 别名]
def handle_abort(self, obj):
"""Handle an incoming ``Data`` abort processing request.
.. IMPORTANT::
This only makes manager's state consistent and doesn't
affect Data object in any way. Any changes to the Data
must be applied over ``handle_update`` method.
:param obj: The Channels message object. Command object format:
.. code-block:: none
{
'command': 'abort',
'data_id': [id of the :class:`~resolwe.flow.models.Data` object
this command was triggered by],
}
"""
async_to_sync(consumer.send_event)(
{
WorkerProtocol.COMMAND: WorkerProtocol.ABORT,
WorkerProtocol.DATA_ID: obj[ExecutorProtocol.DATA_ID],
WorkerProtocol.FINISH_COMMUNICATE_EXTRA: {
"executor": getattr(settings, "FLOW_EXECUTOR", {}).get(
"NAME", "resolwe.flow.executors.local"
),
},
}
)
示例11: connect
# 需要导入模块: from asgiref import sync [as 别名]
# 或者: from asgiref.sync import async_to_sync [as 别名]
def connect(self):
# 创建channels group, 命名为:用户名,并使用channel_layer写入到redis
async_to_sync(self.channel_layer.group_add)(self.scope['user'].username, self.channel_name)
# 返回给receive方法处理
self.accept()
示例12: receive
# 需要导入模块: from asgiref import sync [as 别名]
# 或者: from asgiref.sync import async_to_sync [as 别名]
def receive(self, text_data):
async_to_sync(self.channel_layer.group_send)(
self.scope['user'].username,
{
"type": "user.message",
"text": text_data,
},
)
示例13: disconnect
# 需要导入模块: from asgiref import sync [as 别名]
# 或者: from asgiref.sync import async_to_sync [as 别名]
def disconnect(self, close_code):
async_to_sync(self.channel_layer.group_discard)(self.scope['user'].username, self.channel_name)
# class StatsConsumer(WebsocketConsumer):
#
# def connect(self):
# async_to_sync(self.channel_layer.group_add)(self.scope['user'].username, self.channel_name)
#
# self.accept()
#
# def receive(self, text_data):
# key = '-'.join(('django-mstats-processlist', str(self.scope['user'].uid)))
# cache.set(key, 'start', timeout=None)
# show_processlist.delay(host=text_data, user=self.scope['user'].username, key=key)
#
# async_to_sync(self.channel_layer.group_send)(
# self.scope['user'].username,
# {
# "type": "user.message",
# "text": text_data,
# },
# )
#
# def user_message(self, event):
# self.send(text_data=event["text"])
#
# def disconnect(self, close_code):
# key = '-'.join(('django-mstats-processlist', str(self.scope['user'].uid)))
# cache.set(key, 'end', timeout=None)
# async_to_sync(self.channel_layer.group_discard)(self.scope['user'].username, self.channel_name)
示例14: fetch
# 需要导入模块: from asgiref import sync [as 别名]
# 或者: from asgiref.sync import async_to_sync [as 别名]
def fetch(**kwargs):
params = P(**kwargs)
return async_to_sync(scrapetable.fetch)(params)
示例15: fetch
# 需要导入模块: from asgiref import sync [as 别名]
# 或者: from asgiref.sync import async_to_sync [as 别名]
def fetch(params, input_dataframe):
async def get_input_dataframe():
return input_dataframe
return async_to_sync(urlscraper.fetch)(
params, get_input_dataframe=get_input_dataframe
)