1

Current Code

import multiprocessing as mu
import time

global_array=[]

def add_array1(array):
    while True:
        time.sleep(2.5)
        global_array.append(1)

        print(global_array)

def add_array2(array):
    while True:
        time.sleep(3)
        global_array.append(2)

        print(global_array)

def runInParallel(*fns):
      if __name__=='__main__':
        proc = []
      for fn in fns:
        p = mu.Process(target=fn)
        p.start()
        proc.append(p)
      for p in proc:
        p.join()

runInParallel(
    add_array1(global_array),
    add_array2(global_array)
    )

When running my code above only the first function add_array1() is appending the value to the array and printing instead of both functions providing the wrong output:

[1]
[1,1]
[1,1,1]

When the actual desired output for the following code is:

[1]
[1,2]
[1,2,1]
[1,2,1,2]
3
  • you are calling the functions, you probably need to provide them only: Commented Oct 25, 2020 at 23:17
  • what do you mean? I need them to run simultaneously as well. Commented Oct 25, 2020 at 23:19
  • 1
    runInParallel(add_array1(global_array),add_array2(global_array)) EXEcutes the functions not in parallel Commented Oct 25, 2020 at 23:21

3 Answers 3

3

Your problem is that the function call

runInParallel( add_array1(global_array), add_array2(global_array)) 

executes the functions and provides the return value of the function calls as parameters to runInParallel. As add_array1 is an endless loop, it never returns from the execution. You need to provide your functions as functions - not the returnvalue of the functions as parameters to runInParallel(...)

Start with

runInParallel( add_array1, add_array2) # name of the functions, dont execute em

and change

def runInParallel(*fns): 
    proc = []
    for fn in fns:
        p = mu.Process(target=fn, args=(global_array,)) # provide param here
        p.start()
        proc.append(p)
    for p in proc:
        p.join()

and then fix the "not joining" problem due to your threaded functions never returning.


Example from the official documentation of multiprocessing.Process:

from multiprocessing import Process 
import os

def info(title):
    print(title)
    print('module name:', __name__)
    print('parent process:', os.getppid())
    print('process id:', os.getpid())

# Function name is f
def f(name):
    info('function f')
    print('hello', name)

if __name__ == '__main__':
    info('main line')
    # f is provided, and args is provided - not f("bob")
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()
Sign up to request clarification or add additional context in comments.

3 Comments

so are you suggesting that I don't give it parameters just call the function and let it append to the global_array?
@Philip See the quoted example from the documentation for how to call/provide a function that takes parameters
@philip Your function have endless loops, they will never return - so they will also never join. You need to fix that as well
1

You can use the the code below to get your desired output:

import threading
import time

global_array = []


def add_array1():
    while True:
        time.sleep(2.5)
        global_array.append(1)

        print(global_array)


def add_array2():
    while True:
        time.sleep(3)
        global_array.append(2)

        print(global_array)


def runInParallel(*fns):
    if __name__ == '__main__':
        proc = []
    for fn in fns:
        p = threading.Thread(target=fn)
        p.start()
        proc.append(p)
    for p in proc:
        p.join()


if __name__ == '__main__':
    runInParallel(
        add_array1,
        add_array2
    )

1 Comment

I already have the if __name__=='__main__': in my runInParallell() function.
0

you can use this simple code to get your desired output:

'''

import time


global_array = []


def add_array1(array):


    while True:
        time.sleep(2.5)
        if len(global_array) % 2 == 0:

            global_array.append(1)
            print(global_array)
    
        else:
            global_array.append(2)
            print(global_array)


add_array1(global_array)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.