• Python爬虫--进程和线程2


    多线程

    1、使用threading模块创建线程

    方式一:把函数传入并创建Thread实例,然后调用start方法开始执行
    # coding=utf8
    """
    创建多线程的方式一:
        把函数传入并创建Thread实例,然后调用start方法开始执行
    """
    import random, time
    import threading
    
    def thread_run(urls):
        print 'Current thread %s is running...' % threading.current_thread().name
        for url in urls:
            print '%s -->>> %s' % (threading.current_thread().name, url)
            time.sleep(random.random())
        print '%s ended.' % threading.current_thread().name
    
    if __name__ == '__main__':
        print 'Current thread %s is running...' % threading.current_thread().name
        t1 = threading.Thread(target=thread_run, name='Thread_1', args=(['url_1','url_2','url_3'],))
        t2 = threading.Thread(target=thread_run, name='Thread_2', args=(['url_4','url_5','url_6'],))
        t1.start()
        t2.start()
        t1.join()
        t2.join()
        print '%s ended.' % threading.current_thread().name
    
    
    方式二:继承Thread类,重写__init__和run方法
    # coding=utf8
    '''
    创建多线程方式二:
        继承Thread类,重写__init__和run方法
    '''
    import threading
    import time, random
    
    
    class myThread(threading.Thread):
        def __init__(self, name, urls):
            threading.Thread.__init__(self, name=name)
            self.urls = urls
    
        def run(self):
            print 'Current thread %s is running...' % threading.current_thread().name
            for url in self.urls:
                print '%s -->>> %s' % (threading.current_thread().name, url)
                time.sleep(random.random())
            print '%s ended.' % threading.current_thread().name
    
    if __name__ == '__main__':
        print 'Current thread %s is running...' % threading.current_thread().name
        t1 = myThread(name='Thread_1', urls=['url_1','url_2','url_3'])
        t2 = myThread(name='Thread_2', urls=['url_4','url_5','url_6'])
        t1.start()
        t2.start()
        t1.join()
        t2.join()
        print '%s ended.' % threading.current_thread().name
    

    2、线程同步

    使用Lock和RLock可以实现简单的线程同步,将每次只允许一个线程操作的数据放到acqure和release方法之间

    # coding=utf8
    import threading
    mylock = threading.RLock()
    num = 0
    class myThread(threading.Thread):
        def __init__(self,name):
            threading.Thread.__init__(self, name=name)
    
        def run(self):
            global num
            while True:
                mylock.acquire()
                print '%s locked, Number: %d' % (threading.current_thread().name, num)
                if num >= 4:
                    mylock.release()
                    print '%s released, Number: %d' % (threading.current_thread().name, num)
                    break
                num += 1
                print '%s released, Number: %d' % (threading.current_thread().name, num)
                mylock.release()
    
    if __name__ == '__main__':
        thread1 = myThread('Thread_1')
        thread2 = myThread('Thread_2')
        thread1.start()
        thread2.start()
    

    输出1:

    输出2:

    作者:lykxbg —— 来一块小饼干

    出处:http://www.cnblogs.com/lykxbg/

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    poj 2892 && 3468
    hdu 2444
    反素数
    poj 2406 && 1961
    Java定时器Timer的使用
    Linux环境下如何生成core文件
    Java异步CompletableFuture的使用
    Java中线程池的使用
    Linux系统添加应用服务进程的守护进程
    C++通过Webbrowser调用JavaScript
  • 原文地址:https://www.cnblogs.com/lykxbg/p/11969374.html
Copyright © 2020-2023  润新知