我试图使用部分函数,以便pool.map()可以定位具有多个参数的函数(在本例中为Lock()对象).
这是示例代码(取自我之前的一个问题的答案):
from functools import partial
def target(lock, iterable_item):
for item in items:
# Do cool stuff
if (... some condition here ...):
lock.acquire()
# Write to stdout or logfile, etc.
lock.release()
def main():
iterable = [1, 2, 3, 4, 5]
pool = multiprocessing.Pool()
l = multiprocessing.Lock()
func = partial(target, l)
pool.map(func, iterable)
pool.close()
pool.join()
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此代码时,我收到错误:
Runtime Error: Lock objects should only be shared between processes through inheritance.
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?如何在子进程之间共享锁?
I am currently running some endless tasks using asyncio.wait
I need a special function to run when all the others are on await
import asyncio
async def special_function():
while True:
# does some work,
# Passes control back to controller to run main_tasks
# if they are no longer waiting.
await asyncio.sleep(0)
async def handler():
tasks = [task() for task in main_tasks]
# Adding the task that I want to run when all main_tasks are awaiting:
tasks.append(special_function())
await asyncio.wait(tasks)
asyncio.get_event_loop().run_until_complete(handler()) …Run Code Online (Sandbox Code Playgroud) 我multiprocessing.Pool()用来并行化一些重的Pandas处理,但发现它有点太成功了.我的CPU使用率达到100%,我的整台计算机变得非常无响应.甚至鼠标也变得难以使用.
我可以使用此代码更改进程的进程优先级.
import psutil
p = psutil.Process(os.getpid())
p.nice = psutil.BELOW_NORMAL_PRIORITY_CLASS
Run Code Online (Sandbox Code Playgroud)
但是,当我查看Windows任务管理器时,我发现只有主python.exe进程已更改为低于正常优先级.
有没有一种降低池进程优先级的好方法?