小编Vin*_*ada的帖子

在python3中的线程内运行多个并行作业

我有两个函数func1(),func2()它们相互独立,可以在两个线程中运行。我正在使用threading库python3来运行这两个线程。不过里面func1(),我也运行for使用并行循环joblib。因此,使用线程以及 joblib 会给出以下警告 -

/usr/local/lib/python3.6/dist-packages/joblib/parallel.py:547: UserWarning: Multiprocessing-backed parallel loops cannot be nested below threads, setting n_jobs=1,

然后里面的 for 循环func1()只是按顺序运行。

以下是代码片段的示例:

from joblib import Parallel, delayed
import threading
from math import sqrt

class MyThread(threading.Thread):
    def __init__(self, sample, type):
        threading.Thread.__init__(self)
        self.type = type
        self.sample = sample
    def run(self):
        if self.type=='func1':
            self.sample.func1()
        else:
            self.sample.func2()

class Sample:
   def func1(self):
       print('this function runs for …
Run Code Online (Sandbox Code Playgroud)

parallel-processing python-multithreading joblib python-3.6

5
推荐指数
0
解决办法
530
查看次数