【发布时间】:2021-01-19 02:51:47
【问题描述】:
%%cython
from threading import Thread
import time
def countdown(n):
while n > 0:
n -= 1
COUNT = 10000000
start = time.time()
t1 = Thread(target=countdown,args=(COUNT/2,))
t2 = Thread(target=countdown,args=(COUNT/2,))
with nogil:
t1.start();t2.start()
t1.join();t2.join()
end = time.time()
print(end-start)
我在 Cython 文档网站上读到,几乎每个 Python 代码都可以在 Cython 中使用。众所周知,这是一个著名的 sn-p 来展示 Python 和 GIL 的局限性。我试图在 Cython 中重新创建一个解决方案。
但是,我在编译此代码时遇到了这个问题。
Error compiling Cython file:
------------------------------------------------------------
...
start = time.time()
t1 = Thread(target=countdown,args=(COUNT/2,))
t2 = Thread(target=countdown,args=(COUNT/2,))
with nogil:
t1.start();t2.start()
^
------------------------------------------------------------
/Users/sanskar/.ipython/cython/_cython_magic_bb84bd2392afb02224d35c81782c007c.pyx:14:12: Discarding owned Python object not allowed without gil
Error compiling Cython file:
------------------------------------------------------------
...
从我的代码中可以看出,我对 Cython 还很陌生。我想就如何解决它提出一些建议。
【问题讨论】:
标签: python c python-3.x types cython