无法关闭使用 pycurl 打开的流
我正在使用 pycurl 开发一个 Web 服务客户端。客户端打开与流服务的连接并将其生成到单独的线程中。这是连接设置方式的精简版本:
def _setup_connection(self):
self.conn = pycurl.Curl()
self.conn.setopt(pycurl.URL, FILTER_URL)
self.conn.setopt(pycurl.POST, 1)
.
.
.
self.conn.setopt(pycurl.HTTPHEADER, headers_list)
self.conn.setopt(pycurl.WRITEFUNCTION, self.local_callback)
def up(self):
if self.conn is None:
self._setup_connection()
self.perform()
现在,当我想关闭连接时,如果我调用,
self.conn.close()
我会得到以下异常:
error: cannot invoke close() - perform() is currently running
这在某种程度上是有道理的,连接始终处于打开状态。我一直在寻找,似乎找不到任何方法来规避这个问题并干净地关闭连接。
I am working on a client for a web service using pycurl. The client opens a connection to a stream service and spawns it into a separate thread. Here's a stripped down version of how the connection is set up:
def _setup_connection(self):
self.conn = pycurl.Curl()
self.conn.setopt(pycurl.URL, FILTER_URL)
self.conn.setopt(pycurl.POST, 1)
.
.
.
self.conn.setopt(pycurl.HTTPHEADER, headers_list)
self.conn.setopt(pycurl.WRITEFUNCTION, self.local_callback)
def up(self):
if self.conn is None:
self._setup_connection()
self.perform()
Now, when i want to shut the connection down, if I call
self.conn.close()
I get the following exception:
error: cannot invoke close() - perform() is currently running
Which, in some way makes sense, the connection is constantly open. I've been hunting around and cant seem to find any way to circumvent this problem and close the connection cleanly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来好像您在一个线程中调用 close(),而另一个线程正在执行 Perform()。幸运的是,图书馆会警告你,而不是陷入未知的行为维尔。
您应该只从一个线程使用curl 会话,或者让perform() 线程在对perform() 的调用完成时以某种方式进行通信。
It sounds like you are invoking close() in one thread while another thread is executing perform(). Luckily, the library warns you rather than descending into unknown behavior-ville.
You should only use the curl session from one thread - or have the perform() thread somehow communicate when the call to perform() is complete.
显然,您展示了curl包装类中的一些方法,您需要做的就是让对象自行处理。
并且不要明确调用结束。当对象完成其工作并且删除对它的所有引用时,curl 连接将关闭。
You obviously showed some methods from a curl wrapper class, what you need to do is to let the object handles itself.
and don't call the closing explicitly. When the object finishes its job and all the references to it are removed, the curl connection will be closed.