1

Hello I have got a tough problem because I have to return value from target Process function. I can't use Manager to retrieve value from this because I made assembly method which takes only 5 arguments and it will be hard to remake this.I 've created class which inherit from Process class (Here is the code)

class ProcessWithReturnValue(Process):
     def __init__(self, group=None, target=None, name=None, args=(), kwargs={},
             *, daemon=None):
         super().__init__(group=group, target=target, name=name, args=args, kwargs=kwargs, daemon=daemon)
         self._return = None

     def run(self):
         if self._target:
             self._return = self._target(*self._args, **self._kwargs)

     def join(self):
         super().join()
         return self._return

My code where I'am starting and joining Processes is here:

if(rbValue.get() == 2):
        if (i != numberOfThreads - 1):
            processes.append(ProcessWithReturnValue(target=ThreadFunctionWithArrays, args=(
                i * rowsPerThread, (i + 1) * rowsPerThread, width, copyImgLeft, copyImgRight)))
            processes[i].start()
        else:
            processes.append(ProcessWithReturnValue(target=ThreadFunctionWithArrays, args=(
                i * rowsPerThread, height, width, copyImgLeft, copyImgRight)))
            processes[i].start()

----------------------------------------------------------------------------------------------
for i in range(0, numberOfThreads):
    if pixelArray is None:
        pixelArray = processes[i].join()
    else:
        toAppend = processes[i].join()
        pixelArray = np.append(pixelArray, toAppend, axis=0)

I have no errors but, what .join function returns is None

Here is also python function which I wrote to test this process

def ThreadFunctionWithArrays(startPointY, endPointY, pixelsWidth, pixelsArray1, pixelsArray2):
     numberOfRows = endPointY - startPointY
     pixelArray = GenerateEmptyPartMatrix(pixelsWidth, numberOfRows)
     y = 0
     for j in range(startPointY, endPointY):
         for i in range(0, pixelsWidth):
             r1, g1, b1 = pixelsArray1.getpixel((i, j))
             r2, g2, b2 = pixelsArray2.getpixel((i, j))
             pixelArray[y][i] = (
                 r1 * 0.4561 + g1 * 0.500484 + b1 * 0.176381 - r2 * 0.0434706 - g2 * 0.0879388 - b2 * 0.00155529,
                 - r1 * 0.0400822 - g1 * 0.0378246 - b1 * 0.0157589 + r2 * 0.378476 + g2 * 0.73364 - b2 * 0.0184503,
                 - r1 * 0.0152161 - g1 * 0.0205971 - b1 * 0.00546856 - r2 * 0.0721527 - g2 * 0.112961 + b2 * 1.2264)
         y += 1
     return pixelArray

How should i write a proper class which inherits from Process and can return a value with join() method?

here is link to python process.py: https://github.com/python/cpython/blob/master/Lib/multiprocessing/process.py

EDIT: I am not interested in different approaches for my problem. I want to try override join function.

rpanai
  • 12,515
  • 2
  • 42
  • 64
Gibki
  • 11
  • 4
  • Try looking into a shared variable instead of overriding join: https://stackoverflow.com/questions/10415028/how-can-i-recover-the-return-value-of-a-function-passed-to-multiprocessing-proce – AlyT Jan 02 '20 at 14:45
  • I wrote that I can't use managers and shared variables in this situation. I have to override join – Gibki Jan 02 '20 at 14:47
  • Sorry, I read that you couldn't use manager but didn't think how that would indirectly mean you couldn't use a shared variable either. I guess I'm not sure why it would be hard to add another argument. Do you not have access to all the code you are using? – AlyT Jan 02 '20 at 14:57
  • I have got access, but this was very hard for me to learn assembly with nasmx64 and apply my code to python. Now my assembly code is working and i don't want to change there anything if its not necessary. and also I haven;t got much time because I will have to end my another projects and i have to pass exams at my University :) – Gibki Jan 02 '20 at 15:03
  • You don't need to change your assembly. Just write a wrapper around it. – user2357112 Jan 02 '20 at 15:44
  • I have got full code in assembly with extern functions from C, so how i can make wrapper? – Gibki Jan 02 '20 at 15:50
  • I am facing the same issue... Two things I notices is, when I print out the value in the join function itself, it prints the expected return. The second thing is this works as expected when I use a threading.Thread. – Avi Shah Nov 18 '21 at 03:54

0 Answers0