简体   繁体   中英

How could a function look like, that can add an other function to a list of functions that run parallel in python?

def update():
   while True:
       loadData()

def main():
   doStuff()
   addToParallelFunctions(update)
   doOtherStuff()

if __name__ == '__main__':
   addToParallelFunctions(main)

How could that addToParallelFunctions function look like, so that update runs parallel to main and that I can add other functions to run also parallel to the main ?

I've tried that, but it paused the main function and ran the other function until she was finished.

from multiprocessing import Process

def addProcess(*funcs):
    for func in funcs:
       Process(target=func).start()

I enhanced your example: It works for me. Main script and function continue both.

import time
from multiprocessing import Process

def addProcess(*funcs):
    for func in funcs:
       Process(target=func).start()

def update():
   for i in range(20):
       print(i)
       time.sleep(2)

def main():
   print("start")
   addProcess(update)
   print("Main function continues")

if __name__ == '__main__':
   addProcess(main)
   print("main script continues")

Next time, please post an example that is reproducible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM