Python Forum
Multiprocess not writing to file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiprocess not writing to file
#4
I've got it working with multiprocessing's queue class:
def create_averaging_process(processes, image_file, q):
    p = Process(target=get_averages, args=(image_file, q))
    processes.append(p)
    p.start()

def write_to_file(file, q, end):
    with open(file, 'a') as f:
        while True: 
            line = q.get()
            if line == end:
                return
            f.write(str(line))
            

def get_averages(image, q):
    try:
        img = Image.open(image).convert('RGB')
    except Exception as e:
        print(e)
    img2 = img.resize((1, 1), Image.ANTIALIAS) #easiest way of getting average color - resize to 1x1 with anti-alias                                     
    col = img2.getpixel((0, 0)) #get color of that single pixel
    try:
        something = col[2] #sometimes there are just singular ints
        q.put(col)
    except Exception as e:
        print(e)


if __name__ == "__main__":
    index = 0
    processes = []
    queue = multiprocessing.Queue()

    STOP_TOKEN="end"
    
    allfiles = glob.glob(path+"/images/*")

    while (len(processes)<len(allfiles)):
        if (len(processes) - len([p for p in processes if not p.is_alive()]) < MAX_PROCESSES):
            create_averaging_process(processes, allfiles[index], queue)
            index+=1
   
    writer_process = multiprocessing.Process(target = write_to_file, args=(path+"/test.txt", queue, STOP_TOKEN))
    writer_process.start()
    
    queue.put(STOP_TOKEN)
    writer_process.join()
Reply


Messages In This Thread
RE: Multiprocess not writing to file - by stullis - Dec-05-2019, 11:18 PM
RE: Multiprocess not writing to file - by DreamingInsanity - Dec-06-2019, 06:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,437 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 1,053 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,594 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Writing to file ends incorrectly project_science 4 2,775 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Writing unit test results into a text file ateestructural 3 4,840 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Writing to file in a specific folder evapa8f 5 3,513 Nov-13-2020, 10:10 PM
Last Post: deanhystad
  multiprocess hang when certain number is used in the program esphi 7 3,278 Nov-06-2020, 03:49 PM
Last Post: esphi
  Failure in writing binary text to file Gigux 7 3,894 Jul-04-2020, 08:41 AM
Last Post: Gigux
  writing data to a csv-file apollo 1 2,413 Jul-03-2020, 02:28 PM
Last Post: DeaD_EyE
  Writing to File Issue Flash_Stang 3 2,575 Jun-05-2020, 05:14 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020