whitewinds's recent timeline updates
whitewinds

whitewinds

V2EX member #621024, joined on 2023-03-27 12:01:09 +08:00
Per whitewinds's settings, the topics list is only visible after you sign in
Deals info, including closed deals, is not hidden
whitewinds's recent replies
May 15, 2023
Replied to a topic by d29107d Python 一个关于协程的 Python 面试题
你的这个写法并不正确,要知道 sync_f 里面的代码是会阻塞线程的,如果你 sync_f 和 async_g 都在主线程跑,那么如果 sync_f 先执行,event loop 就要等到 sync_f 解除阻塞才会执行 async_g ,那么 sync_f 和 async_g 并不 parallel ,你现在的代码之所以看起来并行,实际上是 async_g 先执行了,你可以在两个函数的开头写个 print 看看。

调用顺序对 event loop 的影响如下

import asyncio
import time

async def f():
time.sleep(3)

async def g():
await asyncio.sleep(3)

async def main():
start = time.time()
# await asyncio.gather(f(), g()) # cost 6.006543159484863
# await asyncio.gather(g(), f()) # cost 3.0021119117736816
print(f"cost {time.time()-start}")

asyncio.run(main())

所以,这题中 sync_f 必须放到另外的线程执行
miniconda 管理虚拟环境
pip-compile 管理包
我不太喜欢这种透的纸,做笔记很容易透过去很难受
@hongzhangliu 生成器和推导式里面可以只写 if ,当然 if 前面并不是一个 <expression>
(print(1) for i in range(10) if i % 2)
[print(1) for i in range(10) if i % 2]
Apr 12, 2023
Replied to a topic by bestcondition Python 有一个 Python 泛型类型注释的问题
# 扩展 enum
class EnumBase(Generic[GT]): # <---
# 额外对象
extra_obj: GT

def __new__(cls: Type[T], value: str, extra_obj: GT = None,) -> T:
obj = object.__new__(cls) # type: Any
obj._value_ = value
obj.extra_obj = extra_obj
return obj


class B:
def __init__(self, value):
self.value = value


class EnumTest(EnumBase[B], _Enum): # <---
A = 'A', B(1)
B = 'B', B(2)


foo = EnumTest.A.extra_obj
print(foo.value)
Apr 12, 2023
Replied to a topic by ChenJHua Python Python 内存优化问题
你 100 个独立脚本,运行就要 100 个 python 解释器进程,进程之间内存隔离。或者你单独起一个 pymongo 的服务进程,其他脚本进程用 IPC 调它。
读过第一版,是本好书
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4139 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 18ms · UTC 05:20 · PVG 13:20 · LAX 22:20 · JFK 01:20
♥ Do have faith in what you're doing.