在类方法内运行多重处理

问题描述 投票:0回答:1

我正在尝试在类方法内运行多重处理。

我不明白,在池多处理的映射函数内传递类属性/类方法后出现一些错误。我将其移到课堂之外,但仍然出现错误。

我的工作职能是:

def run_a_request(client,  prompt, stream=False):
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {
                "role": "user",
                "content": prompt
            }
        ],
        temperature=0.1,
        stream=stream
    )
    if not stream:
        response = response.choices[0].message.content

    return response

调用功能:

   def run_intermediate_response(self, subgraph, components, question):

        intermediate_response = ""
        prompts = []
        for idx in range(len(components)):
            tmp = dict()
            tmp['components'] = [subgraph['components'][idx]]

            knowledge = self.__graph_linearizor.run(tmp)[0][0]
            prompts.append(generate_prompt_for_global_search(knowledge, question))

        pool = Pool(5)
        result = pool.map(run_a_request, zip(repeat(client), prompts))

        print(result)

        return 1

我收到此错误:

  File "/home/ju/PycharmProjects/kgqa_graphrag/framework/components/graph_retriever/base/base_retrieval/subgraph_retriever.py", line 156, in run_intermediate_response
    result = pool.map(run_a_request, zip(repeat(client), prompts))
  File "/home/ju/anaconda3/envs/kgqa_graphrag/lib/python3.9/multiprocessing/pool.py", line 364, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File "/home/ju/anaconda3/envs/kgqa_graphrag/lib/python3.9/multiprocessing/pool.py", line 771, in get
    raise self._value
  File "/home/ju/anaconda3/envs/kgqa_graphrag/lib/python3.9/multiprocessing/pool.py", line 537, in _handle_tasks
    put(task)
  File "/home/ju/anaconda3/envs/kgqa_graphrag/lib/python3.9/multiprocessing/connection.py", line 206, in send
    self._send_bytes(_ForkingPickler.dumps(obj))
  File "/home/ju/anaconda3/envs/kgqa_graphrag/lib/python3.9/multiprocessing/reduction.py", line 51, in dumps
    cls(buf, protocol).dump(obj)
TypeError: cannot pickle '_thread.RLock' object

有办法解决这个问题吗?谢谢你

python linux multiprocessing
1个回答
0
投票

这与在类方法内部或外部无关。

(大概)

client
对象包含一个无法pickle的
threading.RLock
对象,并且需要跨多处理边界发送的所有内容都需要可pickle。

要解决此问题,请不要将

client
发送给子进程,并让
run_a_request()
函数(例如,可选,如果您需要单处理或其他方式运行它)构造自己的
client
对象.

© www.soinside.com 2019 - 2024. All rights reserved.