concurrency in python

13
Concurrency with Multiprocessing in Python PhillyPug & Philly.rb RedSnake Meeting February 8th, 2011 Gavin M. Roy myYearbook.com

Upload: gavin-roy

Post on 06-May-2015

1.570 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Concurrency in Python

Concurrency with Multiprocessing in Python

PhillyPug & Philly.rb RedSnake MeetingFebruary 8th, 2011

Gavin M. RoymyYearbook.com

Page 2: Concurrency in Python

green threads in threading

Page 3: Concurrency in Python

from threading import Threadimport 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)

Page 4: Concurrency in Python

gmr-0x04:pika gmr$ python threads.py MyThread-1 runningMyThread-2 runningMyThread-3 runningMyThread-4 runningMyThread-5 runningMyThread-6 runningMyThread-7 runningMyThread-8 runningMyThread-9 runningMyThread-10 runningMyThread-1 doneMyThread-2 doneMyThread-3 doneMyThread-4 doneMyThread-5 doneMyThread-6 doneMyThread-7 doneMyThread-8 doneMyThread-9 doneMyThread-10 done

Page 5: Concurrency in Python

threading

• Locks

• Reentrant Locks

• Conditions

• Semaphores

• Events

• Timers

Reentrant knows who own the locks and the recursion level

Additional classes like Queue.Queue

Page 6: Concurrency in Python
Page 7: Concurrency in Python

This GIL From David Beeazley’s GIL Visualizationhttp://www.dabeaz.com/GIL/gilvis/fourthread.html

Page 8: Concurrency in Python

enter multiprocessing

Page 9: Concurrency in Python

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

Page 10: Concurrency in Python

from multiprocessing import Processimport 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)

Page 11: Concurrency in Python

gmr-0x04:pika gmr$ python processes.py Thread-1 runningThread-2 runningThread-3 runningThread-4 runningThread-5 running Thread-6 running Thread-7 runningThread-8 runningThread-9 runningThread-10 runningThread-1 doneThread-2 doneThread-3 doneThread-4 doneThread-5 doneThread-6 doneThread-7 doneThread-8 doneThread-9 doneThread-10 done

Page 12: Concurrency in Python

Photo By Susan NYC: http://www.flickr.com/photos/en321/33868864/

multiprocessing.reduction

Page 13: Concurrency in Python

# Process #1from multiprocessing.reduction import reduce_handleimport socket

# Create a socketsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)

# Do stuff here, client or server wise

# Create the pickled socket handlehandle = reduce_handle(sock.fileno)

# Process #2from multiprocessing.reduction import rebuild_handleimport socket

# In other processfd = rebuild_handle(handle)sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)

# Now I can read and write from the socket too