0

i have now managed to start a function in parallel using multithreading. So far it works quite well.

                thread_list = []
                    for thr in range(threads):
                    thread = threading.Thread(target=Extract_high_vegetation, args=(kachelnummer, thr, os.path.join(output_dir, kachelnummer, kachelnummer + '_input.txt'), os.path.join(output_dir, kachelnummer, 'Output'))
                    thread_list.append(thread)
                    thread_list[thr].start()

                for thread in thread_list:
                thread.join()

Now I would like to process the command in a separate function, so that I can only pass the target function and the argument of the target function in each case.

def multithreading(target_function, argument):
    thread_list = []
    for thr in range(threads):
        thread = threading.Thread(target = target_function, args = argument)
        thread_list.append(thread)
        thread_list[thr].start()

        for thread in thread_list:
            thread.join()

def Extract_high_vegetation(kachelnummer, i, input_LAZ_file, output_folder):
    if not os.path.isdir(output_folder):
        os.makedirs(output_folder)
        kachelnummer = kachelnummer.replace('_', '-')


def Extract_low_vegetation(kachelnummer, i, input_LAZ_file, output_folder):
    if not os.path.isdir(output_folder):
        os.makedirs(output_folder)
        kachelnummer = kachelnummer.replace('_', '-')

multithreading(Extract_high_vegetation, kachelnummer, thr, os.path.join(output_dir, kachelnummer, kachelnummer + '_input.txt'), os.path.join(output_dir, kachelnummer, 'High'))
multithreading(Extract_low_vegetation, kachelnummer, thr, os.path.join(output_dir, kachelnummer, kachelnummer + '_input.txt'), os.path.join(output_dir, kachelnummer, 'Low'))

The two "vegetation" functions are not exhaustive, but are only intended to clarify the arguments. Unfortunately I can't manage to pass the argument, which consists of different variables for the target function. Can someone please tell me how to pass the arguments to the target function? As I understand it, I would have to pack the second part (starting with "tile number") into a single string. Currently there are a lot more of them.

Thanks a lot

  • While it does not answer your question, you might want to take a look at [``concurrent.futures.ThreadPoolExecutor``](https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor). It already provides a what you are trying to implement yourself. – MisterMiyagi Nov 20 '20 at 11:34
  • Does this answer your question? [What does ``**`` (double star/asterisk) and ``*`` (star/asterisk) do for parameters?](https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters) – MisterMiyagi Nov 20 '20 at 11:35

1 Answers1

0

Looks like you are passing arguments individually to multithreading but you need to pass them all as a single tuple.

multithreading(Extract_high_vegetation, (kachelnummer, thr, ...))
multithreading(Extract_low_vegetation, (kachelnummer, thr, ...))
strotter
  • 181
  • 9