栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

AttributeError:无法使用多处理队列来腌制本地对象的计算..function1

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

AttributeError:无法使用多处理队列来腌制本地对象的计算..function1

嵌套函数不是在顶级定义的函数,因此这就是您收到错误的原因。您需要重新定位计算的定义

function1
以及
function2
计算之外。

编写方式时,您的流程将立即启动,而不是在安排它们运行的​​日期开始。那可能会达到您的预期目的:

import osimport timeimport randomfrom multiprocessing import Process, Queuefrom threading import Threadfrom datetime import datetimeimport scheduledef function1(q):    while True:        daydate = datetime.now()        number = random.randrange(1, 215)        fmt = '(pid: {}) Sent to function2: ({}, {})'        print(fmt.format(os.getpid(), daydate, number))        q.put((daydate, number))        time.sleep(2)def function2(q):    while True:        date, number = q.get()        fmt = "(pid: {}) Received values from function1: ({}, {})"        print(fmt.format(os.getpid(), date, number))        # time.sleep(2) no need to sleep here because q.get will block until        # new items are availabledef computation():    q = Queue()    a = Process(target=function1, args=(q,))    a.start()    b = Process(target=function2, args=(q,))    b.start()    a.join()    b.join()if __name__ == "__main__":    # We are spawning new threads as a launching platform for    # computation. Without it, the next job couldn't start before the last    # one has finished. If your jobs always end before the next one should     # start, you don't need this construct and you can just pass     # ...do(computation)    schedule.every().friday.at("01:02").do(        Thread(target=computation).start    )    schedule.every().friday.at("01:03").do(        Thread(target=computation).start    )    while True:        schedule.run_pending()        time.sleep(1)

就像现在一样,一旦启动,您的进程将永远运行。如果这不是您想要的,则必须考虑实现一些停止条件。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/611250.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号