from time import time from lxml html import document_fromstring from Q

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from time import time
from lxml.html import document_fromstring
from Queue import Empty
import multiprocessing
from multiprocessing import Queue
html = open('index.html').read()
def test_multi(n):
q = Queue()
for _ in xrange(n):
q.put(html)
pool = []
for _ in xrange(multiprocessing.cpu_count()):
p = multiprocessing.Process(target=proc, args=(q,))
p.start()
pool.append(p)
for p in pool:
p.join()
def proc(q):
while 1:
try:
html = q.get(block=False)
document_fromstring(html)
except Empty:
break
def test_single(n):
for _ in xrange(n):
document_fromstring(html)
def test(func, n):
t1 = time()
func(n)
return time() - t1
if __name__ == '__main__':
n = int(raw_input('N: ').strip())
print 'SINGLE: ', test(test_single, n)
print 'MULTI: ', test(test_multi, n)