1

I was thinking to use multiprocess package to run a function in parallel but I need to pass a different value of parameter every run (every 1 sec).

e.g.)

def foo(list):
  while True:
    <do something with list>
    sleep(1000)

def main():
  process = multiprocess.Process(target=foo, args=(lst))
  process.start()

  <keep updating lst>

This will cause a foo() function running with the same parameter value over and over. How can I work around in this scenario?

6
  • If you update lst in your parent process, it won't automatically reflect into a multiprocessing child process, so this doesn't help you much... Commented Jan 6, 2023 at 20:06
  • Got it. What other options do I have then? Commented Jan 6, 2023 at 20:15
  • 1
    What do you want to do, in the end? Commented Jan 6, 2023 at 20:15
  • I want to run foo() function concurrently while passing a different param value while main() function updates that parameter variable.. Commented Jan 6, 2023 at 20:21
  • 1
    Yes, but why? What should the function do? Are you maybe actually looking for a threading.Queue or multiprocessing.Queue to pass work with? Commented Jan 6, 2023 at 20:23

2 Answers 2

2

Armed with the knowledge of what you're actually trying to do, i.e.

The foo function does an http post call to save some logs (batch) to the storage. The main function is getting text logs (save log to the batch) while running a given shell script. Basically, I'm trying to do batching for logging.

the answer is to use a thread and a queue for message passing (multiprocessing.Process and multiprocessing.Queue would also work, but aren't really necessary):

import threading
import time
from queue import Queue


def send_batch(batch):
    print("Sending", batch)


def save_worker(queue: Queue):
    while True:
        batch = queue.get()
        if batch is None:  # stop signal
            break
        send_batch(batch)
        queue.task_done()


def main():
    batch_queue = Queue()
    save_thread = threading.Thread(target=save_worker, args=(batch_queue,))
    save_thread.start()
    log_batch = []

    for x in range(42):  # pretend this is the shell script outputting things
        message = f"Message {x}"
        print(message)
        log_batch.append(message)
        time.sleep(0.1)
        if len(log_batch) >= 7:  # could also look at wallclock
            batch_queue.put(log_batch)
            log_batch = []
    if log_batch:
        batch_queue.put(log_batch)  # send the last batch
    print("Script stopped, waiting for worker to finish")
    batch_queue.put(None)  # stop signal
    save_thread.join()


if __name__ == "__main__":
    main()
Sign up to request clarification or add additional context in comments.

Comments

1
import threading
import time

def run_every_second(param):
    # your function code here
    print(param)

# create a list of parameters to pass to the function
params = [1, 2, 3, 4]

# create and start a thread for each parameter
for param in params:
    t = threading.Thread(target=run_every_second, args=(param,))
    t.start()
    time.sleep(1)

# wait for all threads to complete
for t in threads:
    t.join()

This will create a new thread for each parameter, and each thread will run the run_every_second function with the corresponding parameter. The threads will run concurrently, so the functions will be executed in parallel. There will be a 1-second time lapse between the start of each thread.

2 Comments

Quick question. Is there a reason why threading is better than multiprocessing in this scenario?
I didn't really think about it, but in general, if you need to run a large number of concurrent I/O-bound tasks, threading may be a good choice. If you need to run a small number of concurrent CPU-bound tasks, or if you need to run tasks concurrently with tasks written in other languages, multiprocessing may be a better choice.

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.