微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Djnago 后台任务有时不工作

如何解决Djnago 后台任务有时不工作

我正在使用 Django 后台任务。

但问题是有时无法正常工作。任务没有完成。有时该函数未被调用

我的views.py

def process_tasks():
    process_tasks_cmd = "python manage.py process_tasks"
    process_tasks_args = shlex.split(process_tasks_cmd)
    process_tasks_subprocess = subprocess.Popen(process_tasks_args,shell=True)

@api_view(["POST"])
@permission_classes([])
def test(request):
        process_tasks()
        fingerprint_sync(6,5,8)
        return Response(result,status=status.HTTP_200_OK)

任务.py

@background(schedule=django.utils.timezone.Now())
def fingerprint_sync(EmployeeId,finger_activatn,device_code):
    """
        FingerPrint Synchronization for employee
        After Creating FingerPrint,Sync the FingerPrint data to all Device

        Parameters:
        EmployeeId: pk of employee

        Returns:
        Background Process Execute
        if syncing not done- Data added to Synchronization table
    """
    try:
        logging.getLogger("info_logger").info("Triggered Fingerprint sync")
        finger = FingerPrintInfo.objects.filter(~Q(FingerData=None),Employee=EmployeeId,Activation=finger_activatn)\
            .order_by('FingerPrintId')
        logging.getLogger("info_logger").info("Finger data filtered and arranged")
        emp = Employee.objects.get(pk=EmployeeId)
        data = {
            "Employees": {
                "EmployeeId": emp.EmployeeId,"EmployeeCode": emp.EmployeeCode,"EmployeeName": emp.EmployeeName,"EmployeeStatus": emp.EmployeeStatus
            },"FingerPrintId": finger[3].FingerPrintId,"FingerData": bytes(finger[3].FingerData).decode("utf-8")
        }
        logging.getLogger("info_logger").info("Corresponding employee identified")
        emp_device = Device.objects.filter(~Q(DeviceStatus=static_values["const0"]),~Q(DeviceCode=device_code))
        for div in emp_device:
            try:
                device = Device.objects.get(pk=div.deviceid)
                url = protocol + div.DeviceCode + device_api["API4"]
                logging.getLogger("info_logger").info(url)
                resp = requests.post(url,json=data)
                logging.getLogger("info_logger").info(resp)
                if not resp.text.__contains__('200'):
                    synchronization = Synchronization(
                        Employee=emp,Device=div,SyncType=selection_type["Fingerprint"]
                    )
                    synchronization.save()
            except Exception as ex:
                logging.getLogger("info_logger").info(repr(ex))
                logging.getLogger("info_logger").info("Fingerprint not synced in " + div.DeviceCode)
    except Exception as ex:
        logging.getLogger("info_logger").info(repr(ex))

这里的指纹同步方法不是每次都调用。不明白为什么。

我的数据库数据

background_task

所有任务都在这里待处理..没有任务正在完成。

有人可以帮我吗

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。