无法关闭使用 pycurl 打开的流

发布于 2024-09-25 10:16:18 字数 712 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡写薰衣草的香 2024-10-02 10:16:18

听起来好像您在一个线程中调用 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.

清音悠歌 2024-10-02 10:16:18

显然,您展示了curl包装类中的一些方法,您需要做的就是让对象自行处理。

def __del__(self):
    self.conn.close()

并且不要明确调用结束。当对象完成其工作并且删除对它的所有引用时,curl 连接将关闭。

You obviously showed some methods from a curl wrapper class, what you need to do is to let the object handles itself.

def __del__(self):
    self.conn.close()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文