Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Concurrency with Multiprocessing in Python

Gavin M. Roy
February 08, 2011

Concurrency with Multiprocessing in Python

Slides from my lightning talk on concurrency in Python using the Multiprocessing module.

Gavin M. Roy

February 08, 2011
Tweet

More Decks by Gavin M. Roy

Other Decks in Programming

Transcript

  1. from threading import Thread import time class MyThread(Thread): def run(self):

    print "%s running" % self.name time.sleep(5) print "%s done" % self.name for x in xrange(0,10): thread = MyThread() thread.start() thread.join(0)
  2. gmr-0x04:pika gmr$ python threads.py MyThread-1 running MyThread-2 running MyThread-3 running

    MyThread-4 running MyThread-5 running MyThread-6 running MyThread-7 running MyThread-8 running MyThread-9 running MyThread-10 running MyThread-1 done MyThread-2 done MyThread-3 done MyThread-4 done MyThread-5 done MyThread-6 done MyThread-7 done MyThread-8 done MyThread-9 done MyThread-10 done
  3. threading • Locks • Reentrant Locks • Conditions • Semaphores

    • Events • Timers Reentrant knows who own the locks and the recursion level Additional classes like Queue.Queue
  4. multiprocessing module • All the things threading has • Exchanging

    Objects • Queues and Pipes • Shared State • Pipes and Queues • Pools • Connections • Managers • SyncManager • Logging TCP Server coordinating shared objects Process Sync
  5. from multiprocessing import Process import time class MyThread(Process): def run(self):

    print "%s running" % self.name time.sleep(5) print "%s done" % self.name for x in xrange(0,10): thread = MyThread() thread.start() thread.join(0)
  6. gmr-0x04:pika gmr$ python processes.py Thread-1 running Thread-2 running Thread-3 running

    Thread-4 running Thread-5 running Thread-6 running Thread-7 running Thread-8 running Thread-9 running Thread-10 running Thread-1 done Thread-2 done Thread-3 done Thread-4 done Thread-5 done Thread-6 done Thread-7 done Thread-8 done Thread-9 done Thread-10 done
  7. # Process #1 from multiprocessing.reduction import reduce_handle import socket #

    Create a socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) # Do stuff here, client or server wise # Create the pickled socket handle handle = reduce_handle(sock.fileno) # Process #2 from multiprocessing.reduction import rebuild_handle import socket # In other process fd = rebuild_handle(handle) sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) # Now I can read and write from the socket too