- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个多线程程序(大约 20 个线程;生产者/消费者与许多队列的混合)
在其中一个线程中,它从队列中弹出字符串并将其发送到远程程序
# it starts the thread like this
workQ = Queue.Queue()
stop_thr_event = threading.Event()
t = threading.Thread( target=worker, args=(stop_thr_event,) )
# in the thread's worker function
def worker(stop_event):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (myhost, int(myport))
sock.connect(server_address)
while True:
try:
item = workQ.get(timeout=1)
if print_only:
print item
else:
if item.startswith("key:"):
item = "{%s}" % item
sock.sendall(item)
workQ.task_done()
except Queue.Empty, msg:
if stop_event.isSet():
break
间歇性地,我的程序会挂起,没有线程在做任何工作
经过反复试验,我发现我的程序仅在该线程运行时挂起
我唯一的猜测是 sendall() 占用了 GIL 并且我的整个程序挂起
1)这是一个合理的理论吗?
2)如果我的理论是正确的,我该怎么做才能让 sendall() 不占用 GIL?使其成为非阻塞发送?
最佳答案
你错了。没有网络事件持有 GIL,sendall() 也不异常(exception)!
item=workQ.get()
socket.sendall() **# may take long time here.**
workQ.task_done()
因为sendall()可能需要很长时间,而其他使用workQ的线程在你调用task_done()之前不能轮流运行==>这就是为什么看起来你的整个程序都在挂起。
关于python:socket.sendall() 占用了 GIL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39630326/
我已经配置了我的环境,以便我可以将适当制作的 .png 文件加载到如下定义的图像中: boost::gil::rgb8_image_t input; 但是我如何加载任何典型类型的 pn
我已经配置了我的环境,以便我可以将适当制作的 .png 文件加载到如下定义的图像中: boost::gil::rgb8_image_t input; 但是我如何加载任何典型类型的 pn
它本质上是这个问题的延伸 - Usage of threadpoolexecutor in conjunction with cython's nogil 在这种情况下,我的 getArea2() 方
我意识到写信给 gil::color_converted_view不影响底层 View 的数据。我想知道这是否正确? 例如,假设我想编写一个程序,获取红色 channel 的值并将蓝色 channel
我有一个 Python 程序,因为它太大了,这里是它的源代码的链接: Link 当我在 Mac 上运行它时,有时会遇到这个奇怪的异常: Fatal Python error: PyEval_Resto
Wikipedia文章Global interpreter lock指示Raku具有全局解释器锁。 这与Curtis Poe's response到Are any companies planning
如果一个线程正在等待阻塞 I/O,Ruby 互斥体是否允许两个线程同时执行? 这是我对 GIL 如何用于 MRI 的理解。我很好奇互斥锁和 GIL 之间有什么区别吗? 最佳答案 是的,这有效。正因为如
我不太确定GIL是如何实现的,但理论上,如果我使用pyinstaller将python脚本编译成exe ,还会实现GIL吗?有什么办法可以绕过这个吗? 最佳答案 如果您使用 CPython(“标准 P
我将 Python 3.2 嵌入到 C++ 应用程序中,并且我有几个在程序中不同时间运行的子解释器(由 Py_NewInterpreter 创建)。他们在不同时间获取和释放 GIL,但当我想销毁其中一
阅读各种解释 GIS 和 Python 线程的文章后,Are locks unnecessary in multi-threaded Python code because of the GIL?这是
有没有办法剖析 python 进程对 GIL 的使用情况?基本上,我想知道持有 GIL 的时间百分比。该进程是单线程的。 我的动机是我有一些用 Cython 编写的代码,它使用 nogil。理想情况下
我正在重新表述我的问题,因为我认为很多人认为这是“Python 有线程吗”的问题。确实如此,但 CPython 也有 GIL,它在任何给定时间都不会调度多个线程。这使得 CPython 线程对于 CP
我想将我的单线程应用程序与工作线程数分开。只有 1 个问题 - 这个 Action 的表现如何?如果 GIL 阻止 python 同时执行超过 1 个线程,我会有任何 yield 吗? 另一点(从 c
在 python 中,我定义了一个全局变量,它被不同的线程读取/递增。由于 GIL,如果不使用任何类型的锁定机制,这是否会导致问题? 最佳答案 GIL 只要求解释器在另一个线程接管之前完全执行单个字节
假设我有一个线程和程序的主要部分。由于 GIL,一个线程应该一次工作(而不是同时工作)吗?但是,如果其中一个线程是一个无限循环(或两者都是无限循环)怎么办? 这两个进程会并行运行吗? def test
我正在阅读有关 GIL 的内容,但它从未真正指定它是否包含主线程(我假设是这样)。我问的原因是因为我有一个带有修改字典的线程设置的程序。主线程根据玩家输入添加/删除,而线程循环数据更新和更改数据。 然
我试图找到一个函数来告诉我当前线程是否具有全局解释器锁。 Python/C-API 文档似乎没有包含这样的函数。 我目前的解决方案是使用 PyGILState_Ensure() 获取锁,然后使用 Py
CPython 使用 Global Interpreter Lock . Linux 已删除 Big Kernel Lock 的所有痕迹.这些锁的替代品是什么?一个系统如何才能充分利用一个真正的多核或
CPU-bound(计算密集型) 和I/O bound(I/O密集型) 计算密集型任务(CPU-bound) 的特点是要进行大量的计算,占据着主要的任务,消耗CPU资源,一直处于满负荷状态。比如复
我的部分工作需要大量计算,但它们通常相当简单,原则上可以很容易地与 Cython 的 prange 并行完成,需要 nogil。但是,鉴于我尝试编写 Cython 代码,重点是将 cdef 类作为构建
我是一名优秀的程序员,十分优秀!