0

I have done a task of concurrent in Python, as the following code

def fun_x(a, b) :
    for x in range(10000) :
        x = x*x
            for y in range(10000) :
                y = y*y
    return a*x, b*y


with futures.ProcessPoolExecutor() as executor:
    futures_all_data = {executor.submit(fun_x,     # Function name
                                        number_a,   # args 1
                                        number_b,   # args 2
                                        ) : number_a 
                                        for number_a, number_b in arg }
output_final = {}
for future in futures.as_completed(futures_all_data, timeout = None) :
        result_Code, result_content = future.result()
        output_final[result_Code] = result_content

return output_final

Currently, I need to use and class and a subfunction instead of funx, like,

def class_x(object):
    __init__(self, a):
        for x in range(10000):
            x = x*x
        a = a*x
    def fun_y(self, b) :
        for y in range(10000):
            y = y*y
        b = y*b + a

However, I am not sure how to deal with the executor.submit part?

Could you give me some guidance?

Thank you so much!

Carl Zheng
  • 708
  • 2
  • 6
  • 20

1 Answers1

0

I'm not sure what your codes really wanna do, seems that your codes is not in good format. But I have an idea, to make any function to a class

Say we have a function x

def x(args):
    body

to make it a class:

import collections

class X(collections.abc.callable):
    def __call__(self, args):
        body
x = X()

# usage
x(args)

So you don't have to change too many things, and you got an class X, and when you need to call it, you still use x(args)

PS: x is in anstance of class X.

Menglong Li
  • 2,177
  • 14
  • 19
  • Thank you for replying. there are two args both need to input to class_x and func_y separately. I am not sure how you this method to realize. But you give me an idea that I can make a new function to call the class_x and func_y. – Carl Zheng Sep 14 '17 at 15:50
  • Then why not make two separate classes ^_^ – Menglong Li Sep 14 '17 at 15:53
  • Hahaha. This idea is the last choice.╮(╯_╰)╭. I am looking into your method. I appreciate! @Menglong Li – Carl Zheng Sep 14 '17 at 17:18