gpt4 book ai didi

Python 多处理随机且无提示地失败

转载 作者:太空宇宙 更新时间:2023-11-03 17:29:45 24 4
gpt4 key购买 nike

我是一名 php 开发人员,试图使用 python 多处理脚本。它正在创建一个需要超过 40,000 个项目长的队列。队列填满了,但我的进程默默地、随机地终止了。在 2.6 和 2.7 中尝试过。

我已将日志记录贯穿始终以尝试诊断发生的情况,但无济于事。在没有明显数据问题的随机条目上,所有子进程将停止并且主线程将退出?注意:无论起始点如何,失败的都是相同的条目,除非正在读取的条目数少于 20 个。

有时它会执行 150 个条目,有时会执行 50 个条目然后退出。因为我需要它跑 40k,所以这是一个大问题。该脚本执行 20 次左右没有问题,并记录正确的进程启动和退出消息。当失败时,不会记录进程退出。

我的代码太敏感,无法发布,但这是我使用的基本模型,取自多线程教程并转换为多处理模块。基本上,只是将“线程”替换为“多处理”,并使用多处理的队列而不是模块队列。

    import Queue
import threading
import time

exitFlag = 0

class myThread (threading.Thread):
def __init__(self, threadID, name, q):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print "Starting " + self.name
process_data(self.name, self.q)
print "Exiting " + self.name

def process_data(threadName, q):
while not exitFlag:
queueLock.acquire()
if not workQueue.empty():
data = q.get()
queueLock.release()
print "%s processing %s" % (threadName, data)
else:
queueLock.release()
time.sleep(1)

threadList = ["Thread-1", "Thread-2", "Thread-3"]
nameList = ["One", "Two", "Three", "Four", "Five"]
queueLock = threading.Lock()
workQueue = Queue.Queue(10)
threads = []
threadID = 1

# Create new threads
for tName in threadList:
thread = myThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1

# Fill the queue
queueLock.acquire()
for word in nameList:
workQueue.put(word)
queueLock.release()

# Wait for queue to empty
while not workQueue.empty():
pass

# Notify threads it's time to exit
exitFlag = 1

# Wait for all threads to complete
for t in threads:
t.join()
print "Exiting Main Thread"

最佳答案

原来,我不小心在 64 位 Windows 机器上安装了 32 位 python。奇怪的行为随之而来,主要来自多处理模块。卸载 32 位并安装 64 位 python 2.7.10 使其正常工作。

因此,如果您的代码执行方式没有任何意义,则可能您使用了错误的解释器!

关于Python 多处理随机且无提示地失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32059488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com