本文整理匯總了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
)