python 3.5.2中的多线程“aborted(disconnected)”

时间:2017-10-03 04:42:17

标签: python multithreading python-3.x

我写了这个python代码,它运行一个简单的马尔可夫链来生成输出,我想启动相同代码的几个线程,并让它们全部报告回程序的一个中心部分。我已经分配了一个队列来发送消息,一个用于接收它们,一个用于我正在启动的每个线程。在线程的主循环中,我将移动deque中的第一项放入qIn中,并等待来自qOut的提示以产生另一个结果。在程序中心部分的主循环中,每秒随机选择一个线程,并且消耗它发送的数据并发出信号以发送更多。这一切都在运行一个线程时按预期工作,但是一旦我增加线程数(例如3),几秒钟后我收到一条消息“中止(断开连接)”并且执行停止。我在这做错了什么?

import time
from numpy.random import choice
from numpy import array
from collections import OrderedDict, deque
from threading import *
from queue import Queue
import random

class processMarkovChain():
    def __init__(self, qIn, qOut, IOtoCPU=65, IOtoIO=35, CPUtoCPU=75, CPUTtoIO=25, ID=0):

        IOTransitionProbabilities  = OrderedDict( sorted({"CPU":IOtoCPU,  "IO ":IOtoIO  }.items(), key=lambda x: x[0]))
        CPUTransitionProbabilities = OrderedDict( sorted({"CPU":CPUtoCPU, "IO ":CPUTtoIO}.items(), key=lambda x: x[0]))

        self.TP = OrderedDict( sorted({"CPU":CPUTransitionProbabilities,
                                       "IO ":IOTransitionProbabilities}.items(), key=lambda x: x[0]))

        self.state = "CPU"
        self.moves = deque(self.generate_move() for _ in range(50))
        self.ID = ID
        self.qIn = qIn
        self.qOut = qOut

    def generate_move(self):
        draw = choice(list(self.TP.keys()), 1, p=array(list(self.TP[self.state].values()))/100)
        self.state = draw[0]
        return draw[0]

    def count(self, state):
        counter = 0
        for s in self.moves:
            if s != state:
                break
            counter += 1
        return counter

    def run_loop(self):
        while(1):
            time.sleep(1)
            retMove = self.moves.popleft()
            print(self.ID, retMove, self.count(retMove))
            self.qIn.put([retMove, self.count(retMove)], timeout=1000)
            self.qOut.get(timeout=1000)
            self.moves.append(self.generate_move())

numThreads = 1
inQueueList = [Queue() for ID in range(numThreads)]
outQueueList = [Queue() for ID in range(numThreads)]
threadList = [Thread(target=processMarkovChain(ID=ID, 
                                               qIn=inQueueList[ID], 
                                               qOut=outQueueList[ID]).run_loop).start() for ID in range(numThreads)]

while 1:
    time.sleep(1)
    luckyThread = random.randint(0,numThreads-1)
    print(inQueueList[luckyThread].get(timeout=1000))
    outQueueList[luckyThread].put("hello", timeout=1000)

一个线程的示例输出:

0 CPU 1
['CPU', 1]
0 CPU 0
['CPU', 0]
0 IO  1
['IO ', 1]
0 IO  0
['IO ', 0]
0 CPU 2
['CPU', 2]
0 CPU 1
['CPU', 1]
0 CPU 0
['CPU', 0]
0 IO  0
['IO ', 0]
0 CPU 0
['CPU', 0]
0 IO  1
...

三个线程的示例输出:

0 CPU 6
1 IO  0
2 IO  4
['IO ', 4]
['CPU', 6]aborted (disconnected)

0 个答案:

没有答案