
concurrent 这个模块是真正多线程吗?
1 liuxingdeyu 2023-05-05 11:51:22 +08:00 新版本已经去掉 gil 了,话说你为啥要用多线程,为了用 cpu ? |
2 githmb 2023-05-05 11:54:07 +08:00 不是号称胶水语言吗?粘一下 C 语言啊,fork 或者 pthread_create 一下 |
3 roundgis 2023-05-05 12:00:19 +08:00 via Android CPython thread 可是真 thread |
4 westoy 2023-05-05 12:02:38 +08:00 1 GIL 被妖魔化的太厉害了 python 的多线程就是 pthread 只是 python 有 GIL 锁会卡 PyObject 的操作 但是这个主要是卡纯 python 的 PyObject 转 C/CPP 的模块里是可以释放 GIL 的 所以大部分 C/CPP 写的库都是会释放 GIL 的, 包括原生 IO 但是因为早年 linux 没有线程, 之后有段时间线程也是基于 fork 的,2.6 的时候又大改过一次, 所以用的人并不多, 导致主流的 Linux 下面都提倡 IO 异步并发, 计算的直接 fork, 用线程的并不多..... |
5 matrix1010 2023-05-05 12:04:09 +08:00 @liuxingdeyu 你这是大新闻, 没有 GIL 的 Python 可以算作另一种语言了 |
6 sbldehanhan OP @githmb 但是如果线程的那个函数里面又调用了其他 Python 函数,那被调用的函数也得用 C 实现一遍。这样工作量就太大了。 |
7 sbldehanhan OP @liuxingdeyu 为了快,现在太慢了。 |
8 r6cb 2023-05-05 12:32:48 +08:00 |
9 Alias4ck 2023-05-05 12:51:20 +08:00 @liuxingdeyu 并没有 ,faster cpython 组 3.12 目标( https://github.com/faster-cpython/ideas/wiki/Python-3.12-Goals) 中 提到的[pep554]( https://peps.python.org/pep-0554/)也只是一个 draft 而且实现也是仅限于 C API 而且 pep554 还在讨论中( https://discuss.python.org/t/pep-554-multiple-interpreters-in-the-stdlib/24855/12) 另外实现`nogil`的是另一个 pep,有兴趣去看看[pep703]( https://peps.python.org/pep-0703/) 还只是一个 draft 虽然他给出了[实现]( https://github.com/colesbury/nogil)。 |