简体   繁体   中英

Threading And MultiProcessing Combination [Python]

I'm trying to experiment with using both the threading and Multiprocessing module

import multiprocessing as mp
import threading
from threading import Thread
import time

QP = mp.Queue()
Lock = mp.Lock()
Input = 100
Checked = mp.Value("i",0)

class MultiThreading(threading.Thread):
    def __init__(self,QP,Checked):
        threading.Thread.__init__(self)
        self.QP = QP
        self.Checked = Checked
    def run(self):
        global Input
        global Lock
        QueueMode = self.QP.get()
        First,Second = QueueMode
        Lock.acquire()
        print ("{} {} - Has Successfully Done".format(First,Second))
        Checked.value += 1
        time.sleep(0.25)
        Lock.release()
        if not Input == Checked.value:
            t = MultiThreading(QP,Checked)
            t.setDaemon(False)
            t.start()

def MultiCall(QP,Checked):
    for i in range(10):
        t = MultiThreading(QP,Checked)
        t.setDaemon(False)
        t.start()

if __name__ == "__main__":
    for i in range(100):
        QP.put((i,i+i))

    Processes = []
    for i in range(4):
        p = mp.Process(target=MultiCall, args=(QP,Checked))
        Processes.append(p)
        p.start()
        p.join

    while not Input == Checked:
        continue

print ("Task Done")

The code above is what i've been currently working on, when i run the code regularly it gets an error either "unable to start new thread" or it just freezes, however when i use the debugger https://www.onlinegdb.com/online_python_interpreter it works but it is slow, so i'm confused as to what is going on.

Inside "run" method in MultiThreading class make the condition of "if" block as below

if Input > Checked.value:

I am assuming here that you want stop creating further thread once Checked.value reaches Input value. With the existing condition as below it may result in creating infinite number of threads. Because there is possibility that some Threads may have the value as more than hundred and it will be always not equal to 100. So it will end up in creating infinite number of threads

if not Input == Checked.value:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM