21.python-爬虫的基础认知,多线程小例子

 多线程爬虫的方便了,但是只要问题是数据可以因为运行速度问题导致数据紊乱,所以要加上锁机制,改变了全局变量的多线程要加上锁。

1.普通的执行方式,运行共计6秒

import time
def coding():
    for x in range(3):
        print('正在写代码%s'%x)
        time.sleep(1)
def drawing():
    for x in range(3):
        print('正在画图%s'%x)
        time.sleep(1)
def main():
    coding()
    drawing()

if __name__ == '__main__':
    main()

运行结果 

正在写代码0
正在写代码1
正在写代码2
正在画图0
正在画图1
正在画图2

2.多线程的写法,运行共计3秒,为2个线程(函数的写法)

import threading
import time
def coding():
    for x in range(3):
        print('正在写代码%s'%threading.current_thread())
        time.sleep(1)
def drawing():
    for x in range(3):
        print('正在画图%s'%x)
        time.sleep(1)
def main():
    t1 = threading.Thread(target=coding)
    t2 = threading.Thread(target=drawing)
    t1.start()
    t2.start()
    print(threading.enumerate())
if __name__ == '__main__':
    main()

 运行结果

正在写代码<Thread(Thread-1, started 3112)>
正在画图0[<_MainThread(MainThread, started 13828)>, <Thread(Thread-1, started 3112)>, <Thread(Thread-2, started 13412)>]

正在写代码<Thread(Thread-1, started 3112)>
正在画图1
正在写代码<Thread(Thread-1, started 3112)>
正在画图2

类的写法 

import threading
import time
class Coding(threading.Thread):
    def run(self):
        for x in range(3):
            print('正在写代码%s'%threading.current_thread())
            time.sleep(1)
class Drawing(threading.Thread):
    def run(self):
        for x in range(3):
            print('正在画图%s'%threading.current_thread())
            time.sleep(1)
def main():
    t1 = Coding()
    t2 = Drawing()
    t1.start()
    t2.start()
    print(threading.enumerate())
if __name__ == '__main__':
    main()

运行结果

正在写代码<Coding(Thread-1, started 13420)>
正在画图<Drawing(Thread-2, started 2312)>[<_MainThread(MainThread, started 7600)>, <Coding(Thread-1, started 13420)>, <Drawing(Thread-2, started 2312)>]

正在写代码<Coding(Thread-1, started 13420)>正在画图<Drawing(Thread-2, started 2312)>

正在画图<Drawing(Thread-2, started 2312)>
正在写代码<Coding(Thread-1, started 13420)>

3.多线程,线程锁 

import threading
VALUE = 0
def add_value():
    global VALUE
    for x in range(1000000):
        VALUE += 1
    print('value:%d'%VALUE)
def main():
    for x in range(2):
        t = threading.Thread(target=add_value)
        t.start()
if __name__ =="__main__":
    main()

运行结果 

value:1297217
value:1348648

加上线程锁后的,多加上一个gLock

VALUE = 0
gLock = threading.Lock()
def add_value():
    global VALUE
    gLock.acquire()
    for x in range(1000000):
        VALUE += 1
    gLock.release()
    print('value:%d'%VALUE)
def main():
    for x in range(2):
        t = threading.Thread(target=add_value)
        t.start()
if __name__ =="__main__":
    main()

运行结果 

value:1000000
value:2000000

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值