Using Libcloud in multi-threaded and async environments

Libcloud’s primary task is to communicate with different provider APIs usingHTTP. This means most of the work is not CPU intensive, but performing allthose HTTP requests includes a lot of waiting which makes the library I/Obound.

Most of the time you want to perform more operations in parallel or justwant your code to finish faster (for example starting a lot of servers orperiodically polling for node status).

Problems like this are usually solved using threads or async libraries suchas Twisted, Tornado or gevent.

This page contains some information and tips about how to use Libcloud insuch environments.

Libcloud and thread-safety

Important thing to keep in mind when dealing with threads is thread-safety.Libcloud driver instance is not thread safe. This means if you don’t wantto deal with complex (and usually inefficient) locking the easiest solutionis to create a new driver instance inside each thread.

Using Libcloud with gevent

gevent has an ability to monkey patch and replace functions in the Pythonsocket, urllib2, httplib and time module with its ownfunctions which don’t block.

You need to do two things when you want to use Libcloud with gevent:

  • Enable monkey patching
  1. from gevent import monkey
  2. monkey.patch_all()
  • Create a separate driver instance for each Greenlet. This is necessarybecause a driver instance reuses the same Connection class.

For an example see Efficiently download multiple files using gevent.

Using Libcloud with Twisted

Libcloud has no Twisted support included in the core which means you needto be careful when you use it with Twisted and some other async frameworks.

If you don’t use it properly it can block the whole reactor (similar asany other blocking library or a long CPU-intensive task) which means theexecution of other pending tasks in the event queue will be blocked.

A simple solution to prevent blocking the reactor is to run Libcloudcalls inside a thread. In Twisted this can be achieved usingthreads.deferToThread which runs a provided method inside the Twistedthread pool.

The example below demonstrates how to create a new node inside a threadwithout blocking the whole reactor.

  1. from __future__ import absolute_import
  2.  
  3. from pprint import pprint
  4.  
  5. # pylint: disable=import-error
  6. from twisted.internet import defer, threads, reactor
  7. from libcloud.compute.types import Provider
  8. from libcloud.compute.providers import get_driver
  9.  
  10.  
  11. @defer.inlineCallbacks
  12. def create_node(name):
  13. node = yield threads.deferToThread(_thread_create_node,
  14. name=name)
  15. pprint(node)
  16. reactor.stop()
  17.  
  18.  
  19. def _thread_create_node(name):
  20. Driver = get_driver(Provider.RACKSPACE)
  21. conn = Driver('username', 'api key')
  22. image = conn.list_images()[0]
  23. size = conn.list_sizes()[0]
  24. node = conn.create_node(name=name, image=image, size=size)
  25. return node
  26.  
  27.  
  28. def stop(*args, **kwargs):
  29. reactor.stop()
  30.  
  31.  
  32. d = create_node(name='my-lc-node')
  33. d.addCallback(stop) # pylint: disable=no-member
  34. d.addErrback(stop) # pylint: disable=no-member
  35.  
  36. reactor.run()