相关资源,地址
案例-1
import threading
from queue import Queue
from urllib.request import urlopen
ips = ["www.baidu.com",
"www.taobao.com",
"www.huawei.com",
"www.alibaba.com",
"www.meituan.com",
"www.xiaomi.com"]
ports = [80, 443]
class Producer(threading.Thread):
def __init__(self, _queue):
super(Producer, self).__init__()
self.queue = _queue
def run(self):
urls = ["http://%s:%s" % (ip, port) for ip in ips for port in ports]
for url in urls:
self.queue.put(url)
class Consumer(threading.Thread):
def __init__(self, _queue):
super(Consumer, self).__init__()
self.queue = _queue
def run(self):
try:
url = self.queue.get()
urlopen(url)
except Exception as e:
print("%s is unknown url" % url)
else:
print("%s is ok" % url)
if __name__ == "__main__":
# 实例化一个队列
queue = Queue()
for i in range(2):
producer = Producer(queue)
producer.start()
for i in range(30):
consumer = Consumer(queue)
consumer.start()
# output:
# http://www.taobao.com:443 is unknown url
# http://www.huawei.com:443 is unknown url
# http://www.huawei.com:443 is unknown urlhttp://www.taobao.com:443 is unknown url
#
# http://www.baidu.com:80 is ok
# http://www.baidu.com:443 is unknown url
# http://www.xiaomi.com:443 is unknown url
# http://www.baidu.com:80 is ok
# http://www.baidu.com:443 is unknown url
# http://www.xiaomi.com:443 is unknown url
# http://www.alibaba.com:443 is unknown url
# http://www.alibaba.com:443 is unknown url
# http://www.meituan.com:443 is unknown urlhttp://www.meituan.com:443 is unknown url
#
# http://www.huawei.com:80 is ok
# http://www.huawei.com:80 is ok
# http://www.xiaomi.com:80 is ok
# http://www.xiaomi.com:80 is ok
# http://www.taobao.com:80 is ok
# http://www.meituan.com:80 is ok
# http://www.meituan.com:80 is ok
# http://www.taobao.com:80 is ok
# http://www.alibaba.com:80 is ok
# http://www.alibaba.com:80 is ok
案例-2
# -*- coding: utf-8 -*-
import threading
import queue
import random
import time
# 创建一个队列
q = queue.Queue()
# 假定商品序号
item = 0
def producer():
global item
while True:
time.sleep(1)
item = random.randint(1,10)
# 将一个“商品”推到队列中
q.put(item)
print('producer {}th goods append to q.'.format(item))
def consumer():
while True:
# 在队列中删除一个“商品”
item = q.get()
print(threading.current_thread().getName() + 'consumer get {}th goods from q.'.format(item))
q.task_done()
if __name__ == '__main__':
threads_consumer = []
for i in range(3):
t = threading.Thread(target=consumer)
t.start()
threads_consumer.append(t)
thread_producer = threading.Thread(target=producer)
thread_producer.start()
q.join()
for t in threads_consumer:
t.join()
thread_producer.join()
案例-3
from multiprocessing import Queue, Process
import time, random
def producer(name, food, q):
for i in range(4):
time.sleep(random.randint(1,2))
f = '%s生产了%s%s'%(name, i, food)
print(f)
q.put(f)
def consumer(name, q):
while True:
food = q.get()
if food is None:
print('%s没有获取到东西!' %(name))
break
print('\033[31m%s消费了%s\033[0m' %(name, food))
time.sleep(random.randint(1,2))
if __name__ == "__main__":
q = Queue(20)
p1 = Process(target=producer, args=('p1', '包子', q))
p2 = Process(target=producer, args=('p2', '稀饭', q))
p1.start()
p2.start()
c1 = Process(target=consumer, args=('c1',q))
c2 = Process(target=consumer, args=('c2', q))
c1.start()
c2.start()
p1.join()
p2.join()
q.put(None)
q.put(None)