From 25db319ed740d23b98212d49d90d949c0e607306 Mon Sep 17 00:00:00 2001 From: rieuse Date: Tue, 18 Sep 2018 22:20:04 +0800 Subject: [PATCH 1/3] add --- .../socket\347\273\203\344\271\240.py" | 3 ++ ...20\350\241\214\346\227\266\351\227\264.py" | 20 +++++++++ .../\344\277\241\345\217\267\351\207\217.py" | 32 ++++++++++++++ ...77\347\250\213\351\200\232\344\277\241.py" | 42 +++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 "Python\345\220\204\347\247\215\346\250\241\345\235\227\344\275\277\347\224\250/socket\347\273\203\344\271\240.py" create mode 100644 "Python\345\237\272\347\241\200\344\273\243\347\240\201/\344\275\277\347\224\250with\350\257\255\346\263\225\350\256\241\347\256\227\347\250\213\345\272\217\350\277\220\350\241\214\346\227\266\351\227\264.py" create mode 100644 "Python\345\237\272\347\241\200\344\273\243\347\240\201/\344\277\241\345\217\267\351\207\217.py" create mode 100644 "Python\345\237\272\347\241\200\344\273\243\347\240\201/\345\244\232\347\272\277\347\250\213\351\200\232\344\277\241.py" diff --git "a/Python\345\220\204\347\247\215\346\250\241\345\235\227\344\275\277\347\224\250/socket\347\273\203\344\271\240.py" "b/Python\345\220\204\347\247\215\346\250\241\345\235\227\344\275\277\347\224\250/socket\347\273\203\344\271\240.py" new file mode 100644 index 0000000..952820c --- /dev/null +++ "b/Python\345\220\204\347\247\215\346\250\241\345\235\227\344\275\277\347\224\250/socket\347\273\203\344\271\240.py" @@ -0,0 +1,3 @@ +import socket + + diff --git "a/Python\345\237\272\347\241\200\344\273\243\347\240\201/\344\275\277\347\224\250with\350\257\255\346\263\225\350\256\241\347\256\227\347\250\213\345\272\217\350\277\220\350\241\214\346\227\266\351\227\264.py" "b/Python\345\237\272\347\241\200\344\273\243\347\240\201/\344\275\277\347\224\250with\350\257\255\346\263\225\350\256\241\347\256\227\347\250\213\345\272\217\350\277\220\350\241\214\346\227\266\351\227\264.py" new file mode 100644 index 0000000..43c0862 --- /dev/null +++ "b/Python\345\237\272\347\241\200\344\273\243\347\240\201/\344\275\277\347\224\250with\350\257\255\346\263\225\350\256\241\347\256\227\347\250\213\345\272\217\350\277\220\350\241\214\346\227\266\351\227\264.py" @@ -0,0 +1,20 @@ +from contextlib import contextmanager +import time + +@contextmanager +def timer(func): + try: + start = time.time() + yield func() + finally: + end = time.time() + print(end-start) + + +def func(): + for i in range(9999999): + pass + time.sleep(1) + +with timer(func) as timer: + pass \ No newline at end of file diff --git "a/Python\345\237\272\347\241\200\344\273\243\347\240\201/\344\277\241\345\217\267\351\207\217.py" "b/Python\345\237\272\347\241\200\344\273\243\347\240\201/\344\277\241\345\217\267\351\207\217.py" new file mode 100644 index 0000000..9b8db1b --- /dev/null +++ "b/Python\345\237\272\347\241\200\344\273\243\347\240\201/\344\277\241\345\217\267\351\207\217.py" @@ -0,0 +1,32 @@ +import threading +import time + + +class HtmlSppier(threading.Thread): + def __init__(self, url, sem): + super().__init__() + self.sem = sem + self.url = url + + def run(self): + time.sleep(2) + print('download html success') + self.sem.release() + +class UrlProducer(threading.Thread): + def __init__(self,sem): + super().__init__() + self.sem = sem + + + def run(self): + for i in range(20): + self.sem.acquire() + html_thread = HtmlSppier(f'http://www.qq.com/pn={i}',self.sem) + html_thread.start() + + +if __name__ == '__main__': + sem = threading.Semaphore(3) + url_produce = UrlProducer(sem) + url_produce.start() \ No newline at end of file diff --git "a/Python\345\237\272\347\241\200\344\273\243\347\240\201/\345\244\232\347\272\277\347\250\213\351\200\232\344\277\241.py" "b/Python\345\237\272\347\241\200\344\273\243\347\240\201/\345\244\232\347\272\277\347\250\213\351\200\232\344\277\241.py" new file mode 100644 index 0000000..6a4ff19 --- /dev/null +++ "b/Python\345\237\272\347\241\200\344\273\243\347\240\201/\345\244\232\347\272\277\347\250\213\351\200\232\344\277\241.py" @@ -0,0 +1,42 @@ +from threading import Thread, Condition + + +class T1(Thread): + def __init__(self, con): + super().__init__() + self.con = con + + def run(self): + with self.con: + print(1) + self.con.notify() + self.con.wait() + print(3) + self.con.notify() + self.con.wait() + + +class T2(Thread): + def __init__(self, con): + super().__init__() + self.con = con + + def run(self): + with self.con: + self.con.wait() + print(2) + self.con.notify() + self.con.wait() + print(4) + self.con.notify() + + + + +if __name__ == '__main__': + con = Condition() + thread1 = T1(con) + thread2 = T2(con) + + thread2.start() + thread1.start() From 1074a29cd46868308f8680f09e52267158ebd02d Mon Sep 17 00:00:00 2001 From: rieuse Date: Wed, 19 Sep 2018 10:37:41 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\347\272\277\347\250\213\346\261\240.py" | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 "Python\345\237\272\347\241\200\344\273\243\347\240\201/\347\272\277\347\250\213\346\261\240.py" diff --git "a/Python\345\237\272\347\241\200\344\273\243\347\240\201/\347\272\277\347\250\213\346\261\240.py" "b/Python\345\237\272\347\241\200\344\273\243\347\240\201/\347\272\277\347\250\213\346\261\240.py" new file mode 100644 index 0000000..67fe97f --- /dev/null +++ "b/Python\345\237\272\347\241\200\344\273\243\347\240\201/\347\272\277\347\250\213\346\261\240.py" @@ -0,0 +1,67 @@ +from concurrent.futures import ThreadPoolExecutor, as_completed +import threading +import requests +import time + + +def download_html(i): + url = f'https://www.baidu.com/s?ie=UTF-8&wd={i}' + response = requests.get(url).text + return response + +ids = list(range(100)) + +# with ThreadPoolExecutor(max_workers=8) as exe: +# exe.map(download_html,ids) + + +# 其他接口使用: +from concurrent.futures import ThreadPoolExecutor, as_completed,wait + + +executor = ThreadPoolExecutor(max_workers=8) + +# 通过 submit 提交执行的函数到线程中 +task1 = executor.submit(download_html, (1)) +task2 = executor.submit(download_html, (3)) + +# done() 判断 task 是否完成 +print(task1.done()) +time.sleep(4) +print(task1.done()) + +# result() 获取 task 的执行结果 阻塞 +print(task1.result()) + +# cancel() 取消任务,如果任务在执行中或者执行完了是不能取消的 +# 现在线程池是8 两个任务都会被提交任务去执行,如果 max_workers = 1,执行task2.cancel()就会成功取消 +print(task2.cancel()) + + +# as_completed() 获取已经成功的task的返回数据,阻塞 +# as_completed实际上是一个生成器,里面有 yield 会把已经完成的 future (task) 返回结果 +ids = list(range(10)) +all_task = [executor.submit(download_html,(i)) for i in ids] +time.sleep(8) +# 这是异步的,谁完成就处理谁 +for future in as_completed(all_task): + data = future.result() + print(f'html response {data}') + + +# 通过 executor 获取已经完成的task +for data in executor.map(download_html,ids): + print(f'html response {data}') + + +# wait() 等待task完成 +ids = list(range(10)) +all_task = [executor.submit(download_html,(i)) for i in ids] + +# wait 的 return_when 可选项 +FIRST_COMPLETED = 'FIRST_COMPLETED' +FIRST_EXCEPTION = 'FIRST_EXCEPTION' +ALL_COMPLETED = 'ALL_COMPLETED' +_AS_COMPLETED = '_AS_COMPLETED' + +wait(all_task, return_when=ALL_COMPLETED) From cfce2faa26a6739ce63b2fd2b1c0abf6b953a86a Mon Sep 17 00:00:00 2001 From: rieuse Date: Fri, 1 Jan 2021 13:14:53 +0800 Subject: [PATCH 3/3] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新链接 --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 041fa62..10f219b 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,6 @@ * [Python算法学习](https://github.com/rieuse/learnPython/tree/master/Python%E7%AE%97%E6%B3%95):学习Python算法的时候练习的代码,后续会多多练习,暂时时间还不够,暑假继续学习。 * [Python科学计算基础](https://github.com/rieuse/learnPython/tree/master/Python%E7%A7%91%E5%AD%A6%E8%AE%A1%E7%AE%97%E5%9F%BA%E7%A1%80):Python科学计算相关模块学习使用。 * [ScrapyDoutu](https://github.com/rieuse/learnPython/tree/master/ScrapyDoutu):使用Scrapy框架爬取了斗图网站的最新表情图片 -#### 我的微信帐号:r**ieuse** -#### 我的博客地址:**http://bulolo.cn** -#### 我的简书地址:**http://www.jianshu.com/u/28fbebb6ac57** +#### 微信帐号:**rieuse** +#### 微信公众号:**梅花鹿数据** +#### 博客地址:**http://rieuse.com**