如果我有一个应用程序或函数(不能拆分),它使用多线程时速度更快,并且在使用多处理时工作更快,如何在每个进程中启动多个线程?Python:是否有任何意义使用多处理和线程(对于相同的任务)?
编辑:
我想这是可能的,像这样的代码:
class ThreadClass(threading.Thread):
def __init__(self, threads_in_queue, worker_function, *args, **kwargs):
super().__init__()
...
def run(self): # метод должен быть.
while True:
...
q = self.threads_in_queue.get()
self.worker_function(q)
threads_in_queue = multiprocessing.JoinableQueue(maxsize=number_of_threads)
class ProcessClass(...):
def __init__(self, processes_in_queue):
...
def run(self):
while True:
...
q = self.processes_in_queue.get()
threads_in_queue.put(q)
def worker_function(...):
...
for i in number_of_threads:
t = ThreadClass(worker_function, threads_in_queue, arg1, ..., kwarg1=..., ...)
t.setDaemon(True)
t.start()
if __name__ == '__main__':
processes_in_queue = multiprocessing.JoinableQueue()
for i in number_of_processes:
t = ProcessClass(processes_in_queue)
t.daemon = True
t.start()
for thing_to_queue in things_to_queue:
processes_in_queue.put(...)