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