Threads



or
Getting Stuff Done Faster





Mr CPU



Demo

Scraping URLs

from test import threads
urls = threads.sequential(10) 

from test import threads
urls = threads.threaded(10) 

Sanitising Images


from test import threads
unit = threads.sanitise_sync()

from test import threads
unit = threads.sanitise_threaded()

With gevent


from test import threads
unit = threads.gevent_sanitise_sync()

from test import threads
unit = threads.gevent_sanitise_threaded()

Gevent


 def __iter__(self):

    threads = []

    def events(queue, listener):
        for m in listener:
            print "Yielding: %s" % ("event: %s\ndata: %s\n\n" % ("message", m['data']))
            queue.put("event: %s\ndata: %s\n\n" % ("message", m['data']))
            gevent.sleep(0.1)

    def keepalive(queue):
        while True:
            print "keepalive"
            queue.put(": ping\n\n")
            gevent.sleep(25)

    self.threads.append(gevent.spawn(events, self.queue, self.listener))
    self.threads.append(gevent.spawn(keepalive, self.queue))

    while True:
        yield self.queue.get()

*Server-Sent Events on Heroku

Multiprocessing


Avoiding issues with the GIL by spawning multiple Python processes

Threading

By Ruiwen Chua

Threading

99 Sharing 4 Jun 2014

  • 706