I am new at Python and have been trying to use multithreading. There already exists an in-depth comment on Stackoverflow on the topic, but I still have some questions.
The goal of my program is to create and populate an array (although I guess that technically it would have to be called a "list" in Python) and have it sorted by a "divide and conquer" algorithm. Unfortunately, the terms "list" and "array" seem to be conflated by many a user, even though they are not the same. If "array" is used in my comment, please bear in mind that I have posted different code from various resources and have, for the sake of respecting the original author/s, not changed its content.
My code for populating the list count
was quite simple
#!/usr/bin/env python3
count = []
i = 149
while i >= 0:
count.append(i)
print(i)
i -= 1
After that I used this very handy guide on the topic of "divide and conquer" to create two lists for sorting, which were merged later on. My main concern is now how to use those lists correctly with multithreading.
In the earlier mentioned post it was argued that, basically, just a few lines of code were needed to use multithreading:
from multiprocessing.dummy import Pool as ThreadPool
pool = ThreadPool(4)
as well as
results = pool.starmap(function, zip(list_a, list_b))
to pass multiple lists.
I have tried to adapt the code but failed. My function's parameters are def merge(count, l, m, r)
(used to divide the list count
into a left and a right part) and the two temporarily created lists are called L
and R
.
def merge(arr, l, m, r):
n1 = m - l + 1
n2 = r- m
# create temp arrays
L = [0] * (n1)
R = [0] * (n2)
But every time I run the program, it just responds with the following error message:
Traceback (most recent call last):
File "./DaCcountdownTEST.py", line 71, in <module>
results = pool.starmap(merge,zip(L,R))
NameError: name 'L' is not defined
I don't know the cause for my problem.
Any help is greatly appreciated!