0

我正在使用钩针库运行 django 视图,但是我得到 . 我虽然很简单,但没有经验我不知道如何解决它

Internal Server Error: /clocking/request/send_fence_to_device/
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in get_response
    response = middleware_method(request, response)
  File "/usr/local/lib/python2.7/dist-packages/django/middleware/locale.py", line 29, in process_response
    if (response.status_code == 404 and
AttributeError: 'EventualResult' object has no attribute 'status_code'
ERROR:django.request:Internal Server Error: /clocking/request/send_fence_to_device/
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in get_response
    response = middleware_method(request, response)
  File "/usr/local/lib/python2.7/dist-packages/django/middleware/locale.py", line 29, in process_response
    if (response.status_code == 404 and
AttributeError: 'EventualResult' object has no attribute 'status_code'
{'answer': 'ok'}

我的代码视图

@run_in_reactor
def send_fence_to_device (request):
    device_id = request.GET['device_id']
    fence_id = request.GET['fence_id']
    fnc = Fence.objects.get(id=fence_id)
    dev = Device.objects.get(id=device_id)
    try:
        devLog = dev.devicelog_set.filter(device_id=device_id, status = True).order_by('created').reverse()[:1].all()[0]
    except:
        print "error"
        return HttpResponse(simplejson.dumps(0), mimetype='application/json')
    params = simplejson.loads(fnc.other)
    lttdlgtd = simplejson.loads(fnc.points)
    strCommand = ".geo.%s,%s,%s,%s,%s,%s,%s,%s,%s*" % (params['identificator'], fnc.name[:4], round(float(lttdlgtd[0][0]), 4), round(float(lttdlgtd[0][1]), 4), round(float(fnc.radius), 4), params['time_to_arrive'], params['fence_class'], params['tolerance'], 1)
    print strCommand
    creator = ClientCreator(reactor, AMP)
    cmmdDeferred = creator.connectTCP('127.0.0.1', 8750)
    def connected(protocol):
        return protocol.callRemote(
            AmpProcessor,
            proto=str('TELCOMIP'),
            imei=str(devLog.ip),
            ip=str(devLog.ip),
            port=str(devLog.port),
            cmmd=str(strCommand)
            )
    cmmdDeferred.addCallback(connected)
    #def processed(result):
    #    print result
    #cmmdDeferred.addCallback(processed)
    def done(result):
        reactor.stop()
        return HttpResponse(simplejson.dumps(result), mimetype='application/json')
    cmmdDeferred.addCallback(done)
    #return HttpResponse(simplejson.dumps(1), mimetype='application/json')
4

2 回答 2

0

您可能没有调用任何地方wait的结果send_fence_to_device

此外,send_fence_to_device仍然调用reactor.stop可能会破坏事情。

也许先尝试一个更简单的例子会很有用——比如https://pypi.python.org/pypi/crochet上给出的例子。一旦您了解其中的每个部分在做什么,您可能会更轻松地调试更复杂的应用程序。

此外,您的问题不包括http://sscce.org/所以所有这些都是猜测(至少从 Twisted 的一些经验中得知,但钩针方面没有)。将来,请考虑在您的问题中发布http://sscce.org/,这样就可以减少猜测来给出答案。如果您这样做,您可能会发现更多人愿意提供答案,并且答案可能质量更高。

于 2013-07-05T16:53:41.567 回答
0

答案是工厂django函数是两个分离的函数,一个是通用函数,一个是twisted函数,twisted将在reactor中运行,django函数将使用.wait方法调用它

def view_function (request):
    one = request.GET['one']
    two = request.GET['two']
    result = sendMessage(one, two)
    try:
        answer = result.wait(timeout=None)
        return HttpResponse(simplejson.dumps(answer['answer']), mimetype='application/json')
    except TimeoutError:
        return HttpResponse(simplejson.dumps(-1), mimetype='application/json')

@run_in_reactor    
def twisted_function (_one, _two):
    def connected(protocol):
        return protocol.callRemote(
            RemoteFunction,
            one=_one,
            two=_two
            )
    return ClientCreator(reactor, AMP).connectTCP('127.0.0.1', 8750).addCallback(lambda p: connected(p))
于 2013-07-05T21:07:11.183 回答