socket.py上的深层错误:TypeError:需要一个float

时间:2014-01-31 03:28:44

标签: python sockets celery typeerror twilio

我写了一个功能,通过芹菜异步运行任务,在本地进行测试,这一切都很好。运送到我的登台环境,当芹菜尝试使用以下回溯时,它会失败。

我甚至不确定如何调试此错误,因为它正在被celery调用,并且发生在python标准库中。有什么想法吗?

Traceback (most recent call last):
  File "/home/ubuntu/hypnos-venv/local/lib/python2.7/site-packages/celery/app/trace.py", line 238, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/home/ubuntu/hypnos-venv/local/lib/python2.7/site-packages/celery/app/trace.py", line 416, in __protected_call__
    return self.run(*args, **kwargs)
  File "/home/ubuntu/Hypnos/hypnos/recs_jobber/tasks.py", line 5, in send_sms_action
    msg = twilio_client.sms.messages.create(body = sms_action.body, to=sms_action.to_number, from_=TW_NUMBER)
  File "/home/ubuntu/hypnos-venv/local/lib/python2.7/site-packages/twilio/rest/resources/sms_messages.py", line 167, in create
    return self.create_instance(kwargs)
  File "/home/ubuntu/hypnos-venv/local/lib/python2.7/site-packages/twilio/rest/resources/base.py", line 352, in create_instance
    data=transform_params(body))
  File "/home/ubuntu/hypnos-venv/local/lib/python2.7/site-packages/twilio/rest/resources/base.py", line 204, in request
    resp = make_twilio_request(method, uri, auth=self.auth, **kwargs)
  File "/home/ubuntu/hypnos-venv/local/lib/python2.7/site-packages/twilio/rest/resources/base.py", line 129, in make_twilio_request
    resp = make_request(method, uri, **kwargs)
  File "/home/ubuntu/hypnos-venv/local/lib/python2.7/site-packages/twilio/rest/resources/base.py", line 101, in make_request
    resp, content = http.request(url, method, headers=headers, body=data)
  File "/home/ubuntu/hypnos-venv/local/lib/python2.7/site-packages/httplib2/__init__.py", line 1570, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/home/ubuntu/hypnos-venv/local/lib/python2.7/site-packages/httplib2/__init__.py", line 1317, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/home/ubuntu/hypnos-venv/local/lib/python2.7/site-packages/httplib2/__init__.py", line 1252, in _conn_request
    conn.connect()
  File "/home/ubuntu/hypnos-venv/local/lib/python2.7/site-packages/httplib2/__init__.py", line 1017, in connect
    sock.settimeout(self.timeout)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
TypeError: a float is required

使用Celery的rdb进入框架,似乎没有设置套接字超时。

(Pdb) self.timeout
<Unset Timeout Value>

我有什么想法可以解决这个问题?错误来自Twilio - &gt; httplib2 - &gt; socket.py是_socket的包装器。这是我的头脑,甚至不确定如何处理这个问题。

2 个答案:

答案 0 :(得分:2)

默认情况下,Twilio的lib将超时设置为:

class _UnsetTimeoutKls(object):
    """ A sentinel for an unset timeout. Defaults to the system timeout. """
    def __repr__(self):
        return '<Unset Timeout Value>'


# None has special meaning for timeouts, so we use this sigil to indicate
# that we don't care
UNSET_TIMEOUT = _UnsetTimeoutKls()

因此,必须在实例化TwilioRestClient和实际的套接字调用之间发生一些事情,该套接字调用将_UnsetTimeoutKls评估为与None不同的内容。

初始化TwilioRestClient时将超时设置为无似乎可以解决错误:

self.client = TwilioRestClient(TWILIO_SID,
                    TWILIO_AUTH_TOKEN,
                    timeout=None)

答案 1 :(得分:1)

这是发生了什么:

  • 您告诉Twilio库创建短信。
  • Twilio转到httplib以连接到Twilio服务器。
  • httplib,在连接到Twilio服务器时,设置套接字的超时。

唯一的问题是,由于某种原因,倒数第二个堆栈帧中的self.timeout不是float所需的。您可能想尝试在Python调试器下运行您的应用程序,例如:

python -m pdb myapp.py

您将看到一个提示,您可以从中输入run来运行您的应用程序。一旦发生错误,它应该让你回到提示符。然后,您应该可以输入up,直到找到有问题的框架并查看self.timeout是什么。然后,您可能希望环顾四周,看看self.timeout设置的位置以及原因。然后,您应该可以通过修复该问题来解决问题。