<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Python Blog - All about python &#187; Multiprocessing</title>
	<atom:link href="http://www.python-blog.com/tag/multiprocessing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.python-blog.com</link>
	<description>and technologies around</description>
	<lastBuildDate>Tue, 27 Jul 2010 19:58:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>multiprocessing port scanner</title>
		<link>http://www.python-blog.com/2009/07/17/multiprocessing-port-scanner/</link>
		<comments>http://www.python-blog.com/2009/07/17/multiprocessing-port-scanner/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 14:55:04 +0000</pubDate>
		<dc:creator>Marcin Kuźmiński</dc:creator>
				<category><![CDATA[Multiprocessing]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[cores]]></category>
		<category><![CDATA[GIL]]></category>
		<category><![CDATA[global interpreter lock]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[pararell calculations]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[port scan]]></category>
		<category><![CDATA[port scanner]]></category>
		<category><![CDATA[python blog]]></category>
		<category><![CDATA[socket]]></category>
		<category><![CDATA[Threads]]></category>

		<guid isPermaLink="false">http://www.python-blog.com/?p=125</guid>
		<description><![CDATA[I&#8217;d started to play with multiprocessing module that came up with python 2.6. Multiprocessing module is very similar to threading (it has almost the same functions/classes that threading).
Here are by my opinion three advantages over threading.

Multiprocessing runs on processes not threads.
Overcomes the GIL (global interpreter lock) that threading is using by using sub processes.
Processes can [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d started to play with multiprocessing module that came up with python 2.6. Multiprocessing module is very similar to threading (it has almost the same functions/classes that threading).</p>
<p>Here are by my opinion three advantages over threading.</p>
<ul>
<li>Multiprocessing runs on processes not threads.</li>
<li>Overcomes the GIL (global interpreter lock) that threading is using by using sub processes.</li>
<li>Processes can be synchronized even remotely so we could write a concurrent calculations over the network</li>
</ul>
<p>I made a simple example class that uses multiprocessing module to scan ports, just for testing i made the same thing that Lukasz made using threading. This example however is not more efficient than the one presented in <a href="http://www.python-blog.com/2009/07/01/python-threaded/" target="_blank">http://www.python-blog.com/2009/07/01/python-threaded/</a> but when we could replace the function check_port with more CPU consuming function we could end up with performance grater by the number of cpus/cores we have. For example in one of my projects recently i made a calculations for popular gambling game in Poland, MultiMulti. I&#8217;d made up a calculations of most repeating number in last 50 games. To calculate combination of 9&#8217;s over 20&#8217;s 50 times with threading i&#8217;d got around 1200s with multiprocessing i was able to calculate it in around 700 s with my Core2Duo CPU. I wish i had core2 quad to check the performance :) So i&#8217;f you need to make some heavy calculations multiprocessing can give you that performance.</p>
<p>Here&#8217;s the code and you can download the port descriptions file <a href="http://www.python-blog.com/wp-content/uploads/2009/07/port_list.data">port_list</a> to match port with description.</p>
<pre class="brush:python">from multiprocessing import Process, Queue, cpu_count, Lock
import socket, sys

class PortScanner(object):
    ''' multiprocessing port scanner with port description'''

    def __init__(self, host = '' , port_range = (1, 100), nr_processes = cpu_count(), port_list_file = ''):
        '''
        port_range=(start,stop) default 1,100
        nr_processes = int default cpu_count() '''
        q = Queue()
        l = Lock()
        port_list = []

        try:
            for i in open(port_list_file).readlines():
                port_list.append([x.strip() for x in i.split('\t')])

        except IOError:
            print 'no port list file specified'
            pass

        for _ in xrange(port_range[0], port_range[1]):
            q.put((host, _))

        #to stop all processes we have to put STOP to queue and break the loop for each process
        for _ in xrange(nr_processes):
            q.put('STOP')

        for _ in xrange(nr_processes):
            p = Process(target = self.check_port, args = (q, l, port_list))
            p.start()

    def check_port(self, q, l, port_list):
        ''' worker class invoked by process '''
        while True:
            queue_ret = q.get()

            if queue_ret == 'STOP':
                break

            s = socket.socket()

            try:

                s.connect((queue_ret))

                #lock for uncorrupted printing to console
                l.acquire()
                print "[INFO] %s on port %s is open" % (queue_ret)

                if len(port_list) &gt; 1:
                    for i in port_list:
                        if int(i[0]) == int(queue_ret[1]):
                            for _ in i:sys.stdout.write(_ + " ")
                            print "\n\n"

                l.release()
            except socket.error:
                #print "[WARNING] %s on port %s is closed" % (queue_ret)
                pass
            s.close()

if __name__ == "__main__":

    PortScanner(host = 'example.com', port_range = (1, 60), nr_processes = 40, port_list_file = 'port_list.data')</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.python-blog.com/2009/07/17/multiprocessing-port-scanner/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>multiprocessing get number of cpu&#8217;s</title>
		<link>http://www.python-blog.com/2009/07/13/multiprocessing-get-number-of-cpus/</link>
		<comments>http://www.python-blog.com/2009/07/13/multiprocessing-get-number-of-cpus/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 22:04:13 +0000</pubDate>
		<dc:creator>Marcin Kuźmiński</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Threads]]></category>
		<category><![CDATA[cores]]></category>
		<category><![CDATA[Multiprocessing]]></category>
		<category><![CDATA[number of cores]]></category>
		<category><![CDATA[number of processors]]></category>
		<category><![CDATA[python blog]]></category>
		<category><![CDATA[python multiprocessing]]></category>

		<guid isPermaLink="false">http://www.python-blog.com/?p=120</guid>
		<description><![CDATA[I always wanted to know if there a way to find out how many cpu&#8217;s does the machine have. For example to run n number of processes based on number of cores, recently i was exploring multiprocessing module and i found that there is a method that show  up number of cpu&#8217;s / cores that [...]]]></description>
			<content:encoded><![CDATA[<p>I always wanted to know if there a way to find out how many cpu&#8217;s does the machine have. For example to run n number of processes based on number of cores, recently i was exploring multiprocessing module and i found that there is a method that show  up number of cpu&#8217;s / cores that the machine have.</p>
<pre class="brush:python">
import multiprocessing

multiprocessing.cpu_count()
# this shows up 2 for my core2duo e9300

#so now if we iterate this like that

for i in xrange(multiprocessing.cpu_count()):
    print 'core',i+1

#this prints
#core 1
#core 2
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.python-blog.com/2009/07/13/multiprocessing-get-number-of-cpus/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
