How to use the asgiref.sync.async_to_sync function in asgiref

To help you get started, we’ve selected a few asgiref examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github OpenSlides / OpenSlides / tests / test_case.py View on Github external
def get_last_autoupdate(self, user=None):
        """
        Get the last autoupdate as (changed_data, deleted_element_ids) for the given user.
        changed_elements is a dict with element_ids as keys and the actual element as value
        user_id=None if for full data, 0 for the anonymous and regular ids for users.
        """
        user_id = None if user is None else user.id
        current_change_id = async_to_sync(element_cache.get_current_change_id)()
        _changed_elements, deleted_element_ids = async_to_sync(
            element_cache.get_data_since
        )(user_id=user_id, change_id=current_change_id)

        changed_elements = {}
        for collection, elements in _changed_elements.items():
            for element in elements:
                changed_elements[get_element_id(collection, element["id"])] = element

        return (changed_elements, deleted_element_ids)
github and-sm / testgr / loader / signals.py View on Github external
result['aborted'] = str(job_object.tests.filter(status=6).count())
        return result

    if created:
        channel_layer = get_channel_layer()
        async_to_sync(channel_layer.group_send)(
            "job_tests_details" + "-" + instance.job.uuid,
            {
                "type": "message",
                "message": data()
            }
        )

    if instance:
        channel_layer = get_channel_layer()
        async_to_sync(channel_layer.group_send)(
            "job_tests_details" + "-" + instance.job.uuid,
            {
                "type": "message",
                "message": data()
            }
github welliamcao / OpsManage / apps / cicd / consumers.py View on Github external
def connect(self):
        self.project = self.get_apps(self.scope['url_route']['kwargs']['id'])
        
        if self.project:
            self.group_name = self.scope['url_route']['kwargs']['group_name']
            
            async_to_sync(self.channel_layer.group_add)(self.group_name, self.channel_name)
 
            self.accept()            
        else:
            self.close()
github xufqing / rest_xops / apps / utils / websocket_tail.py View on Github external
def send_message(self, user, message):
        channel_layer = get_channel_layer()
        async_to_sync(channel_layer.group_send)(user, {"type": "user.message", 'message': message})
github leffss / devops / util / callback.py View on Github external
def v2_playbook_on_stats(self, stats):
        hosts = sorted(stats.processed.keys())
        data = format('<code style="color: #FFFFFF">PLAY RECAP ', '*&lt;150') + '</code>'
        data2 = format('PLAY RECAP ', '*&lt;150') + '\r\n\r\n'
        delay = round(time.time() - self.start_time, 6)
        self.res.append(json.dumps([delay, 'o', data2]))
        self.message['status'] = 0
        self.message['message'] = data
        message = json.dumps(self.message)
        async_to_sync(channel_layer.group_send)(self.group, {
            "type": "send.message",
            "text": message,
        })
        for h in hosts:
            s = stats.summarize(h)
            data = '<code style="color: #FFFFFF">{} : ok={} changed={} unreachable={} failed={} skipped={}</code>'.format(
                self.hosts2[h], s['ok'], s['changed'], s['unreachable'], s['failures'], s['skipped'])
            data2 = '{} : ok={} changed={} unreachable={} failed={} skipped={}\r\n\r\n'.format(
                self.hosts2[h], s['ok'], s['changed'], s['unreachable'], s['failures'], s['skipped'])
            delay = round(time.time() - self.start_time, 6)
            self.res.append(json.dumps([delay, 'o', data2]))
            self.message['status'] = 0
            self.message['message'] = data
            message = json.dumps(self.message)
            async_to_sync(channel_layer.group_send)(self.group, {
                "type": "send.message",
github cvisionai / tator / main / consumers.py View on Github external
def _summary(self):
        """Broadcasts progress summary and stores message in redis.
        """
        num_procs = self.rds.hlen(self.gid + ':started')
        num_complete = self.rds.hlen(self.gid + ':done')
        msg = {
            **self.group_header,
            'num_procs': num_procs,
            'num_complete': num_complete,
        }
        if num_procs &gt;= num_complete:
            async_to_sync(self.channel_layer.group_send)(self.prog_grp, msg)
        if num_procs &lt;= num_complete:
            self.rds.hdel(self.latest_grp, self.gid)
            self.rds.delete(self.gid + ':started')
            self.rds.delete(self.gid + ':done')
        else:
            self.rds.hset(self.latest_grp, self.gid, json.dumps(msg))
github welliamcao / OpsManage / apps / apply / consumers.py View on Github external
def disconnect(self, close_code):
        self.sshRbt.stop()       
        async_to_sync(self.channel_layer.group_discard)(self.group_name, self.channel_name)
github lazzyfu / AuditSQL / apps / orders / utils / task.py View on Github external
def pull_msg(self, msg):
        # 推送消息
        msg = f"{msg} \n"
        pull_msg = {'status': 3, 'data': msg}
        async_to_sync(channel_layer.group_send)(self.user, {"type": "user.message",
                                                            'text': json.dumps(pull_msg)})
github OpenSlides / OpenSlides / openslides / utils / autoupdate.py View on Github external
for element in elements:
            if element.get("reload"):
                model = get_model_from_collection_string(element["collection_string"])
                try:
                    instance = model.objects.get(pk=element["id"])
                except model.DoesNotExist:
                    # The instance was deleted so we set full_data explicitly to None.
                    element["full_data"] = None
                else:
                    element["full_data"] = instance.get_full_data()

        # Save histroy here using sync code.
        save_history(elements)

        # Update cache and send autoupdate using async code.
        async_to_sync(async_handle_collection_elements)(elements)