Python —— (4)多线程

线程 & 进程

thread类

threading模块

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
#coding=utf-8

import threading
import time
import requests
import sys

def funl():
time_start = time.time()
r = requests.get(url='http://www.baidu.com')
times = time.time()=time_start
sys.stdout.write('Status:%s---%s---%s\n'%(r.status_code, times, time.strftime('%H:%M:%S')))

def main():
threads = []

threads_count = 10

for i in range(threads_count):
t = threading.Thread(target=funl, args=())
threads.append(t)

for i in range(threads_count):
threads[i].start()

for i in range(threads_count):
threads[i].join()

if __name__ == '__main__':
main()
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
#coding=utf-8

import threading
import Queue

class DoRun(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self._queue = queue

def run(self):
while not self._queue.empty():
ip = self._queue.get()
print ip


def main():
threads = []
threads_count = 10
queue = Queue.Queue()


for i in range(1, 255):
queue.put('106.42.25.' + str(i))

for i in range(threads_count):
threads.append(DoRun(queue))

for i in threads:
i.start()
for i in threads:
i.join()

if __name__ == '__main__':
main()

refer:Python教程 - 廖雪峰的官方网站

梦想还是要有的,万一实现了呢?