https://hackernoon.com/why-is-python-so-slow-e5074b6fe55b 笔记。
GIL 用于解决多线程操作引用计数的难题。副作用是Python无法同时运行多个线程。
JS使用标记清除GC ;同时 JS 也是单线程异步的语言。
# -*- coding: utf-8 -*- | |
""" | |
Three threads print A B C in order. | |
""" | |
from threading import Thread, Condition | |
condition = Condition() |
# -*- coding: utf-8 -*- | |
import redis | |
import random | |
import string | |
import time | |
COUNT = 1_000_000 | |
r = redis.Redis(host="localhost", port=6379, db=0) |
# -*- coding: utf-8 -*- | |
import time | |
from concurrent.futures import ThreadPoolExecutor | |
pool = ThreadPoolExecutor(max_workers=5) | |
def sleep(): | |
time.sleep(20) |
# -*- coding: utf-8 -*- | |
def func(**kwargs): | |
print(kwargs) | |
func(**{"foo": "bar"}) | |
func(**{"foo": "bar"}, **{"hello": "world"}) | |
# PEP 0448 |
In [4]: def func(a, b): | |
...: a, b = b ,a | |
...: | |
In [5]: dis.dis(func) | |
2 0 LOAD_FAST 1 (b) | |
2 LOAD_FAST 0 (a) | |
4 ROT_TWO | |
6 STORE_FAST 0 (a) | |
8 STORE_FAST 1 (b) |
https://hackernoon.com/why-is-python-so-slow-e5074b6fe55b 笔记。
GIL 用于解决多线程操作引用计数的难题。副作用是Python无法同时运行多个线程。
JS使用标记清除GC ;同时 JS 也是单线程异步的语言。
# -*- coding: utf-8 -*- | |
TYPE = { | |
("ALARM_ANALYSIS", "报警分析"), | |
("DAILY_REPORT", "日报") | |
} | |
for k, v in TYPE: | |
print(k, v) |
# -*- coding: utf-8 -*- | |
""" | |
Since we have GIL, should we add Lock for global variable read/write? | |
""" | |
import threading | |
shared_resource_with_no_lock = 0 | |
COUNT = 100000 |
➜ tmp curl -Iv http://ruanyifeng.com/ | |
* Trying 23.251.96.135... | |
* TCP_NODELAY set | |
* Connected to ruanyifeng.com (23.251.96.135) port 80 (#0) | |
> HEAD / HTTP/1.1 | |
> Host: ruanyifeng.com | |
> User-Agent: curl/7.54.0 | |
> Accept: */* | |
> | |
< HTTP/1.1 404 Not Found |
➜ [takachiho] takachiho git:(feat/locator) ipython | |
Python 3.6.5 (default, May 24 2018, 20:41:21) | |
Type 'copyright', 'credits' or 'license' for more information | |
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help. | |
In [1]: x = 1 | |
In [2]: [x for x in range(10)] | |
Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |