简体   繁体   English

如何将未腌制的对象作为参数传递给multiprocessing.Process?

[英]How to pass unpickled object as argument in multiprocessing.Process?

I am unpickling an object and passing as one of the args for process.Getting no output. 我正在摘除一个对象并作为process的args之一传递。没有输出。

I want to know if this method of unpickling and passing an object as argument is causing any error for multiprocessing. 我想知道这种取消拾取和将对象作为参数传递的方法是否会引起多处理错误。 What will be work around for this? 为此将有什么解决方法?

from multiprocessing import Process, Queue
def func(arg1, arg2,q):
    df_temp = arg1[arg1['col'].isin(arg2)]
    q.put(df_temp)

if __name__ == '__main__':
    import pickle
    import pandas as pd
    arg1= pickle.load(open('paths.p','rb'))
    arg2= pd.Series(pd.date_range(end = 'some_Date', periods=12,freq = 'MS')).dt.to_pydatetime()
    arg2=[i.date() for i in arg2]
    q = Queue()
    p =Process(target=func, args=(arg1,arg2,q))
    p.start()
    p.join()    
    while not q.empty():
        w=q.get() 

Your are deadlocking for other reasons. 您因其他原因陷入僵局。

By default if a process is not the creator of the queue then on exit it will attempt to join the queue's background thread. 默认情况下,如果进程不是队列的创建者,那么在退出时它将尝试加入队列的后台线程。 The process can call cancel_join_thread() to make join_thread() do nothing. 该进程可以调用cancel_join_thread()使join_thread()不执行任何操作。 docs 文档

Your Process won't exit because it is joining a feeder thread from multiprocessing.Queue , which gets started as soon as you queue.put() the first time. 您的Process不会退出,因为它正在从multiprocessing.Queue加入一个供料器线程,该线程将在您第一次queue.put()立即启动。 You need to queue.get() in your parent before you join the process. 加入流程之前,您需要在父级中使用queue.get()

Warning: As mentioned above, if a child process has put items on a queue (and it has not used JoinableQueue.cancel_join_thread), then that process will not terminate until all buffered items have been flushed to the pipe. 警告:如上所述,如果子进程已将项目放入队列中(并且未使用JoinableQueue.cancel_join_thread),则该进程将不会终止,直到所有缓冲的项目都已刷新到管道中为止。 This means that if you try joining that process you may get a deadlock unless you are sure that all items which have been put on the queue have been consumed. 这意味着,如果您尝试加入该进程,则除非您确定已耗尽所有已放入队列的项目,否则可能会陷入僵局。 Similarly, if the child process is non-daemonic then the parent process may hang on exit when it tries to join all its non-daemonic children. 同样,如果子进程是非守护进程,则当父进程尝试加入其所有非守护进程子进程时,其父进程可能会在退出时挂起。 docs 文档

Also don't use while not q.empty() , it's an anti-pattern which will lead to deadlocks as soon as you have multiple consumers. 也不要在不使用while not q.empty() ,这是一种反模式,一旦有多个使用者,就会导致死锁。 Use sentinel-values instead to notify the consumer that no further items are coming. 请使用哨兵值来通知消费者不再有其他物品。 More about this, here . 有关此的更多信息,请点击此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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