Python: How can I stop Threading / Multiprocessing from using 100% of my processor?

I have a code that reads data from 7 devices every second for an infinite amount of time. Each cycle creates a thread that starts 7 processes. After completing each process, the program waits 1 second and starts again. Here is the code snippet:

def all_thread(): #function that handels the threading
    thread = threading.Thread(target=all_process) #prepares a thread for the devices
    thread.start() #starts a thread for the devices

def all_process(): #function that prepares and runs processes
    processes = [] #empty list for the processes to be stored
    while len(gas_list) > 0: #this gaslist holds the connection information for my devices
        for sen in gas_list: #for each sen(sensor) in the gas list
            proc = multiprocessing.Process(target=main_reader, args=(sen, q)) #declaring a process variable that sends the gas object, value and queue information to reading function
            processes.append(proc) #adding the process to the processes list
            proc.start() #start the process
        for sen in processes: #for each sensor in the processes list
            sen.join() #wait for all the processes to complete before starting again
        time.sleep(1) #wait one second

However, this uses 100% of my processor. Is this a construct of multithreading and multiprocessing or just bad coding? Is there a way to limit CPU usage? Thank!

Update:

main_reader(), . , , , . , tkinter.

def main_reader(data, q): #this function reads the device which takes less than a second
    output_list = get_registry(data) #this function takes the device information, reads the registry and returns a list of data
    q.put(output_list) #put the output list into the queue
+4
1

, main_reader , , .

multiprocessing.Pool. . , , . , , (. ).

from multiprocessing import Pool, Manager
from time import sleep
import threading
from random import random

gas_list = [1,2,3,4,5,6,7,8,9,10]

def main_reader(sen, rqu):
    output = "%d/%f" % (sen, random())
    rqu.put(output)


def all_processes(rq):
    p = Pool(len(gas_list) + 1)
    while True:
        for sen in gas_list:
            p.apply_async(main_reader, args=(sen, rq))

        sleep(1)

m = Manager()
q = m.Queue()
t = threading.Thread(target=all_processes, args=(q,))
t.daemon = True
t.start()

while True:
    r = q.get()
    print r

, . 10 . . , 10 , , main_reader. 100%, .

, ? , , , - . - , ?

+2

Source: https://habr.com/ru/post/1683343/


All Articles