我不知道这里发生了什么。我有一个简单的 django 网站,我在其中登录并填写包含一些信息(设备地址、电源插座 1/2、开/关)的基本表格。当我提交表单时,RQ 将此任务(使用请求向 URL 发出 HTTP GET 请求)提交到队列中并执行。我在函数的末尾放了一个返回“作业完成”,我看到作业正在完成,结果是“作业完成”。因此该函数正在执行,但由于某种原因,它跳过了所有 if/elif 块。查看 django-rq 仪表板和工作控制台输出,看起来参数传递正确。如果我将函数复制/粘贴到 python 窗口中,它会在我传递相同的参数时成功运行。
视图.py
@login_required(login_url="/login/")
def index(request):
if request.method == 'POST':
form = APIForm(request.POST)
if form.is_valid():
queue = django_rq.get_queue('default')
queue.enqueue(api_call, form.cleaned_data['device'], form.cleaned_data['socket'], form.cleaned_data['change_to'])
obj = SocketChange()
obj.device = form.cleaned_data['device']
obj.socket = form.cleaned_data['socket']
obj.change_to = form.cleaned_data['change_to']
obj.submitter = request.user
obj.save()
return HttpResponseRedirect(reverse('log'))
else:
return render(request, 'api/index.html', {'form': form})
else:
form = APIForm()
return render(request, 'api/index.html', {'form': form})
任务.py
@job
def api_call(device, socket, change_to):
basicAuthUser = 'user'
basicAuthPassword = 'pass'
#
# port: socket to be controlled
# ctrl_kind: 1 = outlet on, 2 = outlet off, 3 = outlet reboot, 4 = outlet reboot
# status value (Ret): 0 = outlet off, 1 = outlet on, 2 = switching on, 3 = switching off
#
if socket == "1" and change_to == 'True':
url = "https://{0}/out_ctrl.csp?port=1&ctrl_kind=1".format(device)
response = requests.get(url, auth=HTTPBasicAuth(basicAuthUser, basicAuthPassword), verify=False)
response = response.json()
if response['OutCtrl'][0]['Ret'] == 1:
return "Socket 1 is on!"
else:
return "Error!"
elif socket == "1" and change_to == 'False':
url = "https://{0}/out_ctrl.csp?port=1&ctrl_kind=2".format(device)
response = requests.get(url, auth=HTTPBasicAuth(basicAuthUser, basicAuthPassword), verify=False)
response = response.json()
if response['OutCtrl'][0]['Ret'] == 0:
return "Socket 1 is off!"
else:
return "Error!"
elif socket == "2" and change_to == 'True':
url = "https://{0}/out_ctrl.csp?port=2&ctrl_kind=1".format(device)
response = requests.get(url, auth=HTTPBasicAuth(basicAuthUser, basicAuthPassword), verify=False)
response = response.json()
if response['OutCtrl'][0]['Ret'] == 1:
return "Socket 2 is on!"
else:
return "Error!"
elif socket == "2" and change_to == 'False':
url = "https://{0}/out_ctrl.csp?port=2&ctrl_kind=2".format(device)
response = requests.get(url, auth=HTTPBasicAuth(basicAuthUser, basicAuthPassword), verify=False)
response = response.json()
if response['OutCtrl'][0]['Ret'] == 0:
return "Socket 2 is off!"
else:
return "Error!"
return "JOB FINISHED"
工人输出:
14:22:06 *** Listening on default...
14:22:06 Sent heartbeat to prevent worker timeout. Next one should arrive within 420 seconds.
14:27:09 default: api.tasks.api_call('test-device', '1', 'False') (cb9f4827-d06c-471b-a81b-2960e983dda0)
14:27:09 Sent heartbeat to prevent worker timeout. Next one should arrive within 420 seconds.
14:27:10 Sent heartbeat to prevent worker timeout. Next one should arrive within 420 seconds.
Current job: cb9f4827-d06c-471b-a81b-2960e983dda0
14:27:10 default: Job OK (cb9f4827-d06c-471b-a81b-2960e983dda0)
14:27:10 Result: 'JOB FINISHED'
14:27:10 Result is kept for 500 seconds