【问题标题】:AttributeError: 'EventualResult' object has no attribute 'status_code'AttributeError:“EventualResult”对象没有属性“status_code”
【发布时间】:2013-07-05 14:42:29
【问题描述】:

我正在使用钩针库运行 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')

【问题讨论】:

    标签: django twisted


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

      答案是工厂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))
      

      【讨论】:

        猜你喜欢
        • 2018-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-29
        • 2022-11-03
        相关资源
        最近更新 更多