Python线程让每个线程按顺序处理各自的编号

Python线程让每个线程按顺序处理各自的编号,python,multithreading,python-3.x,python-multithreading,Python,Multithreading,Python 3.x,Python Multithreading,我希望第一个线程处理第一个、第五个和第九个,第二个线程处理第二个、第六个、第十个、第三个、第七个、第十一个,第四个线程处理第八个和第十二个。我知道这是一个重复的模式(4*计数器+原始),但当涉及到移动实际的线程进程时,我就不知所措了。这是我到目前为止所拥有的。 如果我做错了,他们就告诉我,因为我愿意接受建议 编辑-我正在使用Python 3.3 def calc(threadName): testRange = 100 testNumber = 100 timesToTe

我希望第一个线程处理第一个、第五个和第九个,第二个线程处理第二个、第六个、第十个、第三个、第七个、第十一个,第四个线程处理第八个和第十二个。我知道这是一个重复的模式(4*计数器+原始),但当涉及到移动实际的线程进程时,我就不知所措了。这是我到目前为止所拥有的。 如果我做错了,他们就告诉我,因为我愿意接受建议

编辑-我正在使用Python 3.3

def calc(threadName):
    testRange = 100
    testNumber = 100
    timesToTest = 25
    testCounter = 0
    if threadName  == 'ThreadOne':
        testNumber = (testNumber)   + 5*(testCounter)
    if threadName == 'ThreadTwo':
        testNumber = (testNumber+1) + 5*(testCounter)
    if threadName == 'ThreadThree':
        testNumber = (testNumber+2) + 5*(testCounter)
    if threadName == 'ThreadFour':
        testNumber = (testNumber+3) + 5*(testCounter)

    while testCounter < timesToTest:
        testCounter +=1
        while testRange >= 0:
            answer = ((testNumber*3) - ((testNumber-1)**2))
            testbool = isprime(answer)
            print('Testing '+str(testNumber)+' on '+str(threadName))
            testNumber +=1
            testRange -= 1
            if testbool:
                list.append((threadName,testNumber,answer))


threadOne = _thread.start_new_thread(calc,('ThreadOne', ))
threadTwo = _thread.start_new_thread(calc,('ThreadTwo', ))
threadThree = _thread.start_new_thread(calc,('ThreadThree', ))
threadFour = _thread.start_new_thread(calc,('ThreadFour', ))

while 1:
    pass
def calc(螺纹名称):
测试范围=100
testNumber=100
timesToTest=25
testCounter=0
如果threadName==“ThreadOne”:
testNumber=(testNumber)+5*(测试计数器)
如果threadName==“ThreadTwo”:
testNumber=(testNumber+1)+5*(testCounter)
如果threadName=='ThreadThree':
testNumber=(testNumber+2)+5*(测试计数器)
如果threadName==“ThreadFour”:
testNumber=(testNumber+3)+5*(testCounter)
当testCounter=0时:
答案=((测试编号*3)-(测试编号-1)**2))
testbool=isprime(答案)
打印('Testing'+str(testNumber)+'on'+str(threadName))
testNumber+=1
测试范围-=1
如果testbool:
追加((线程名称、测试编号、答案))
threadOne=_thread.start_new_thread(计算,('threadOne',))
threadTwo=\u thread.start\u new\u thread(计算,('threadTwo',))
threadThree=\u thread.start\u new\u thread(计算,('threadThree',))
threadFour=_thread.start_new_thread(计算,('threadFour',))
而1:
通过
我试过这个:

import threading
import queue

class Worker(threading.Thread):
    global results_list
    print('in main class')
    def __init__(self, name):
        threading.Thread.__init__(self)
        self.name = name
        self.jobs_queue = queue.Queue()
        self.results_list = list()
        print('in init')

    def isprime(self,n):
        n = abs(int(n))
        print('in isprime')
        if n < 2:
            return False
        if n == 2:
            return True

        if not n & 1:
            return False

        for x in range(3, int(n**0.5)+1, 2):
            if n % x == 0:
                return False
        return True

    def run(self):
        print('in run')
        while True:
            testNumber = self.jobs_queue.get()
            if testNumber == "END":
                return
                # here, do your stuff with 'testNumber'
            # for example, let's multiply it by 2
            answer = ((testNumber**3) - ((testNumber-1)**3))
            testbool = self.isprime(answer)
            if testbool:# results are appended to a list
                self.results_list.append((self.name,testNumber,answer))
    def calc(self, n):
        print('in calc')
        self.jobs_queue.put(n)
        if not self.is_alive():
            self.start()
    def get_result(self):
        print('in get_result')
        return self.results_list
    def stop(self):
        print('in stop')
        # tell the thread to stop,
        # once jobs in queue are done
        self.jobs_queue.put("END")
        self.join()
print('Anything')
workers = [Worker('thread 1'), Worker('thread 2'), Worker('thread 3'), Worker('thread 4')]

for n in range(100):
    print('here 1')
    w = workers[n % 4]
    w.calc(n)

for w in workers:
    w.stop()

for w in workers:
    x=1
    # print(results_list)
导入线程
导入队列
类工作线程(threading.Thread):
全球成果清单
打印('在主类中')
定义初始化(self,name):
threading.Thread.\uuuuu init\uuuuuu(自)
self.name=名称
self.jobs_queue=queue.queue()
self.results\u list=list()
打印('in init')
def iPrime(自我,n):
n=abs(int(n))
打印(“在iPrime中”)
如果n<2:
返回错误
如果n==2:
返回真值
如果不是n&1:
返回错误
对于范围(3,int(n**0.5)+1,2)内的x:
如果n%x==0:
返回错误
返回真值
def运行(自):
打印('运行中')
尽管如此:
testNumber=self.jobs\u queue.get()
如果testNumber==“END”:
返回
#在这里,使用“testNumber”完成您的工作
#例如,让我们将其乘以2
答案=((测试编号**3)-(测试编号-1)**3))
testbool=self.isprime(答案)
如果testbool:#结果将附加到列表中
self.results\u list.append((self.name、testNumber、answer))
def计算(自身,n):
打印('计算中')
self.jobs_queue.put(n)
如果不是self,你还活着吗()
self.start()
def get_结果(自身):
打印('in get_result')
返回self.results\u列表
def停止(自):
打印('in stop')
#告诉线程停止,
#一旦队列中的作业完成
self.jobs\u queue.put(“结束”)
self.join()
打印('任何')
Worker=[Worker('thread1')、Worker('thread2')、Worker('thread3')、Worker('thread4')]
对于范围(100)内的n:
打印('此处为1')
w=工人[n%4]
w、 计算(n)
对于在职工人:
w、 停止()
对于在职工人:
x=1
#打印(结果列表)

据我所知,您需要一个由4个工作线程组成的池,公平地排队等待相同的“作业” 在这4个线程之间

我会做更多类似的事情:

import threading
import queue

class Worker(threading.Thread):
    def __init__(self, name):
        threading.Thread.__init__(self)
        self.name = name
        self.jobs_queue = queue.Queue()
        self.results_list = list()
    def run(self):
        while True:
            testNumber = self.jobs_queue.get()
            if testNumber == "END":
                return
            # here, do your stuff with 'testNumber'
            # for example, let's multiply it by 2
            answer = testNumber * 2
            # results are appended to a list
            self.results_list.append((self.name,testNumber,answer))
    def calc(self, n):
        self.jobs_queue.put(n)
        if not self.is_alive():
            self.start()
    def get_result(self):
        return self.results_list
    def stop(self):
        # tell the thread to stop,
        # once jobs in queue are done
        self.jobs_queue.put("END")
        self.join()

workers = [Worker('thread 1'), Worker('thread 2'), Worker('thread 3'), Worker('thread 4')]

for n in range(100):
    w = workers[n % 4]
    w.calc(n)

for w in workers:
    w.stop()

for w in workers:
    print(w.get_result())

它不起作用,我对它们几乎一无所知。我使用了线程模块来远离线程模块。感谢您的快速回复,但我不知道这里发生了什么。为什么要远离线程模块?什么不起作用?你能告诉我错误信息是什么吗。你到底不明白什么?我很乐意再解释一点。我试图避免,因为这是我第一次尝试线程程序,找不到3.3的任何教程或示例。我试图使用线程,因为它是基本问题的基本解决方案。另外,它不工作,因为在最后的语法是不兼容的3.3I编辑的代码,测试它;它与3.3兼容;我补充了一些意见。希望你能理解。不。。。可能您会感到困惑,因为结果是按每个线程显示的(线程1的所有结果,然后是线程2的所有结果,等等)。不管怎么说,代码并不多,我给你的建议是稍微研究一下,你会明白的。很抱歉