2

I am writing an application using pycurl, and need to make it work in Twisted. I've been searching for either making pycurl somehow compatible with Twisted framework, or using an existing Twisted library. I am suggested Twisted web, but there is no direct map of functions from pycurl to Twisted web. Can anyone point me to the right direction?

Edit: One solution is run pycurl in another thread, but preferably, I wish to use Twisted framework or pycurl that is nonblocking, so I don't have to create another thread.

Pippi
  • 2,451
  • 8
  • 39
  • 59

2 Answers2

2

For any blocking function, if there's no other asynchronous alternative, Twisted lets you run it on another thread but treat it as a Deferred.

from twisted.internet import threads

d = threads.deferToThread(pycurl.some_function)
d.addCallback(callback)

See Integrating blocking code with Twisted in Generatin Deferreds.

cdonts
  • 9,304
  • 4
  • 46
  • 72
2

Have you considered using Twisted web.client.Agent? It is very basic agent, but integrates very well with the Twisted event loop.

keturn
  • 4,780
  • 3
  • 29
  • 40
lipponen
  • 733
  • 1
  • 5
  • 18
  • There's also [treq](http://treq.readthedocs.org/en/latest/), for an interface which some prefer over the basic twisted.web.client interface. – keturn Sep 04 '15 at 19:50