import asyncio
from pyppeteer import launch
from queue import Queue
from threading import Thread
async def run(q):
browser = await launch(headless=False)
page = await browser.newPage()
while True:
tmp = q.get()
print(tmp)
print(f'消费产品——{tmp}')
q.task_done()
await page.setViewport({'width': 1920, 'height': 1080})
await page.setJavaScriptEnabled(enabled=False)
await page.evaluateOnNewDocument('Object.defineProperty(navigator, "webdriver", {get: () => undefined})')
url = 'http://baidu.com'
print('32行', tmp)
if tmp == "结束":
await browser.close()
break
await page.goto(url, timeout=100000)
print('跳出了循环')
def produce(q, num_runs):
for i in range(1, 11):
q.put(i)
print(f'生产产品——{i}')
print('生产者线程结束')
for _ in range(num_runs):
q.put("结束")
async def main(num_runs):
q = Queue()
pro = Thread(target=produce, args=(q, num_runs))
pro.start()
semaphore = asyncio.Semaphore(num_runs) # 定义数量为num_runs的信号量
async def worker():
async with semaphore:
await run(q)
tasks = []
for _ in range(num_runs):
tasks.append(asyncio.create_task(worker()))
await asyncio.gather(*tasks)
print('36')
pro.join()
print('jion')
asyncio.run(main(5))
上一篇
python代码美化工具autopep8
python代码美化工具autopep8
2020-02-18
下一篇
图片追加写入word文档
图片追加写入word文档
2020-02-18