0

example:

random_list = ['foo','foo','foo','foo',56,'foo','foo','foo','foo','foo']
# in random_list there are always 10 elements, one of which is 56, but its position is unknown 

def find_index_of_56_in_list_1():
    for index in range(0,5):
        if 56 == random_list[index]:
            return index

def find_index_of_56_in_list_2():
    for index in range(6,10):
        if 56 == random_list[index]:
            return index

I would like to split the computational power of iterating in the list in 2 Thread one for the first half of the list 'find_index_of_56_in_list_1' and the other for the second half of the list 'find_index_of_56_in_list_2'. and if one process has terminated then the other must terminate as well and nothing or return 'none'.

The problem I am trying to solve is not this one but a bigger one, this example is an analogy. I've tried with threading library, but i can't get one process to terminate when the other has terminated or vice versa.

Mezza
  • 23
  • 3
  • You should show what u tried with the threading library – mont_ Oct 19 '22 at 12:10
  • 3
    Threading doesn't speed up computation itself - you can use threading to speed up processes that have to wait for external resources, or that depend on external processes to progress. But if it's just some computational operation, threads won't help, you need to look at parallel processes. – Grismar Oct 19 '22 at 12:11
  • 1
    What benefit do you perceive when you `"split the computational power of iterating"`? – quamrana Oct 19 '22 at 12:12
  • look at multiprocessing module , Managers.event . You can share info among processes, thus raising a request to other process to safely exit when needed. – murari prasad Oct 19 '22 at 12:13
  • @Mezza due to something called Global Interpreter Lock present in Python, you are very unlikely to get a speedup in such a scenario, and the added overhead of multiprocessing might actually **slow down** the program. – matszwecja Oct 19 '22 at 12:16
  • Consider iterating in a back-and-forth manner in a single loop: `for index in chain.from_iterable(zip(range(0,5), range(6,10))`. This will check index 0, 6, 1, 7, 2, 8, ..., simulating concurrent searching through the two halves of the list. – chepner Oct 19 '22 at 12:23
  • But since you don't know where 56 is, this is asymptotically no better than just doing a linear search. – chepner Oct 19 '22 at 12:26
  • @matszwecja what you say is true even if the list was much much bigger than the example? – Mezza Oct 19 '22 at 13:10
  • @Mezza okay, so it's gonna very heavily depend on the exact specifics of what you are doing inside but if it's something like finding an index of a specific element (the crux of the issue is that processes need to get their set of data to work on, which must be as independent of each other as possible - communicating between processes is costly) than yes, you might see some speed up for big enough set of data – matszwecja Oct 19 '22 at 13:22
  • @Mezza You might want to check out [this question](https://stackoverflow.com/questions/47903791/how-to-terminate-a-multiprocess-in-python-when-a-given-condition-is-met) – matszwecja Oct 19 '22 at 13:23

2 Answers2

0

I feel like you could find some answers in recursion. The question is: do you want to?

Jokes aside, most search algorithms use some form of recursion, and they perform really well. Your example makes me think of binary search -> you take your list, go to the middle point. If it's not the value you're searching, you go to the middle value of the left list or the right list. Keep doing this until you find the value, then return it

0

Can try using thread, I like to use python-worker (link)

from worker import worker

random_list = ['foo','foo','foo','foo',56,'foo','foo','foo','foo','foo']
# in random_list there are always 10 elements, one of which is 56, but its position is unknown 

@worker
def find_index_of_56_in_list_1():
    for index in range(0,5):
        if 56 == random_list[index]:
            return index

@worker
def find_index_of_56_in_list_2():
    for index in range(6,10):
        if 56 == random_list[index]:
            return index

def main():
    worker1 = find_index_of_56_in_list_1()
    worker2 = find_index_of_56_in_list_2()

    result = None

    while worker1.is_alive and worker2.is_alive:
        if worker1.finished:
            print("worker 1 finished first")
            worker2.abort()
            result = worker1.ret
        if worker2.finished:
            print("worker 2 finished first")
            worker1.abort()
            result = worker2.ret
    
    return result
danangjoyoo
  • 350
  • 1
  • 6