当前位置: 首页 > news >正文

济宁市网站建设_网站建设公司_ASP.NET_seo优化

建设网站要做的工作,网站名称是网址吗,杭州pc网站开发公司有哪些,群晖怎么做网站在本文[1]中#xff0c;我们将学习如何使用多处理模块中的特定 Python 类#xff08;进程类#xff09;。我将通过示例为您提供快速概述。 什么是多处理模块#xff1f; 还有什么比从官方文档中提取模块更好的方式来描述模块呢#xff1f; Multiprocessing 是一个使用类似… 在本文[1]中我们将学习如何使用多处理模块中的特定 Python 类进程类。我将通过示例为您提供快速概述。 什么是多处理模块 还有什么比从官方文档中提取模块更好的方式来描述模块呢 Multiprocessing 是一个使用类似于线程模块的 API 支持生成进程的包。多处理包提供本地和远程并发通过使用子进程而不是线程有效地回避全局解释器锁。 线程模块不是本文的重点但总而言之线程模块将处理一小段代码执行轻量级且具有共享内存而多处理模块将处理程序执行较重且完全隔离 。 一般来说多处理模块提供了各种其他类、函数和实用程序可用于处理程序执行期间执行的多个进程。如果程序需要在其工作流程中应用并行性该模块专门设计为交互的主要点。我们不会讨论多处理模块中的所有类和实用程序而是将重点关注一个非常具体的类即进程类。 什么是进程类 在本节中我们将尝试更好地介绍进程是什么以及如何在 Python 中识别、使用和管理进程。正如 GNU C 库中所解释的“进程是分配系统资源的基本单位。每个进程都有自己的地址空间和通常一个控制线程。一个进程执行一个程序可以让多个进程执行相同的程序程序但每个进程在其自己的地址空间内都有自己的程序副本并独立于其他副本执行它。” 但这在 Python 中是什么样子的呢到目前为止我们已经设法对进程是什么、进程和线程之间的区别进行了一些描述和参考但到目前为止我们还没有触及任何代码。好吧让我们改变一下用 Python 做一个非常简单的流程示例 #!/usr/bin/env pythonimport os# A very, very simple process.if __name__  __main__:    print(fHi! Im process {os.getpid()}) 这将产生以下输出 [r0x0dfedora ~]$ python /tmp/tmp.iuW2VAurGG/scratch.pyHi! Im process 144112 正如您所看到的任何正在运行的 Python 脚本或程序都是它自己的一个进程。 创建子进程 那么在父进程中生成不同的子进程又如何呢好吧要做到这一点我们需要多处理模块中的 Process 类的帮助它看起来像这样 #!/usr/bin/env pythonimport osimport multiprocessingdef child_process():    print(fHi! Im a child process {os.getpid()})if __name__  __main__:    print(fHi! Im process {os.getpid()})    # Here we create a new instance of the Process class and assign our    # child_process function to be executed.    process  multiprocessing.Process(targetchild_process)    # We then start the process    process.start()    # And finally, we join the process. This will make our script to hang and    # wait until the child process is done.    process.join() 这将产生以下输出 [r0x0dfedora ~]$ python /tmp/tmp.iuW2VAurGG/scratch.pyHi! Im process 144078Hi! Im a child process 144079 关于上一个脚本的一个非常重要的注意事项如果您不使用 process.join() 来等待子进程执行并完成那么该点的任何其他后续代码将实际执行并且可能会变得有点难以同步您的工作流程。 考虑以下示例 #!/usr/bin/env pythonimport osimport multiprocessingdef child_process():    print(fHi! Im a child process {os.getpid()})if __name__  __main__:    print(fHi! Im process {os.getpid()})    # Here we create a new instance of the Process class and assign our    # child_process function to be executed.    process  multiprocessing.Process(targetchild_process)    # We then start the process    process.start()    # And finally, we join the process. This will make our script to hang and    # wait until the child process is done.    #process.join()    print(AFTER CHILD EXECUTION! RIGHT?!) 该代码片段将产生以下输出 [r0x0dfedora ~]$ python /tmp/tmp.iuW2VAurGG/scratch.pyHi! Im process 145489AFTER CHILD EXECUTION! RIGHT?!Hi! Im a child process 145490 当然断言上面的代码片段是错误的也是不正确的。这完全取决于您想要如何使用该模块以及您的子进程将如何执行。所以要明智地使用它。 创建各种子进程 如果要生成多个进程可以利用 for 循环或任何其他类型的循环。它们将允许您创建对所需流程的尽可能多的引用并在稍后阶段启动/加入它们。 #!/usr/bin/env pythonimport osimport multiprocessingdef child_process(id):    print(fHi! Im a child process {os.getpid()} with id#{id})if __name__  __main__:    print(fHi! Im process {os.getpid()})    list_of_processes  []    # Loop through the number 0 to 10 and create processes for each one of    # them.    for i in range(0, 10):        # Here we create a new instance of the Process class and assign our        # child_process function to be executed. Note the difference now that        # we are using the args parameter now, this means that we can pass        # down parameters to the function being executed as a child process.        process  multiprocessing.Process(targetchild_process, args(i,))        list_of_processes.append(process)    for process in list_of_processes:        # We then start the process        process.start()        # And finally, we join the process. This will make our script to hang        # and wait until the child process is done.        process.join() 这将产生以下输出 [r0x0dfedora ~]$ python /tmp/tmp.iuW2VAurGG/scratch.pyHi! Im process 146056Hi! Im a child process 146057 with id#0Hi! Im a child process 146058 with id#1Hi! Im a child process 146059 with id#2Hi! Im a child process 146060 with id#3Hi! Im a child process 146061 with id#4Hi! Im a child process 146062 with id#5Hi! Im a child process 146063 with id#6Hi! Im a child process 146064 with id#7Hi! Im a child process 146065 with id#8Hi! Im a child process 146066 with id#9 数据通信 在上一节中我描述了向 multiprocessing.Process 类构造函数添加一个新参数 args。此参数允许您将值传递给子进程以在函数内部使用。但你知道如何从子进程返回数据吗 您可能会认为要从子级返回数据必须使用其中的 return 语句才能真正检索数据。进程非常适合以隔离的方式执行函数而不会干扰共享资源这意味着我们知道从函数返回数据的正常且常用的方式。在这里由于其隔离而不允许。 相反我们可以使用队列类它将为我们提供一个在父进程与其子进程之间通信数据的接口。在这种情况下队列是一个普通的 FIFO先进先出具有用于处理多处理的内置机制。 考虑以下示例 #!/usr/bin/env pythonimport osimport multiprocessingdef child_process(queue, number1, number2):    print(fHi! Im a child process {os.getpid()}. I do calculations.)    sum  number1  number2    # Putting data into the queue    queue.put(sum)if __name__  __main__:    print(fHi! Im process {os.getpid()})    # Defining a new Queue()    queue  multiprocessing.Queue()    # Here we create a new instance of the Process class and assign our    # child_process function to be executed. Note the difference now that    # we are using the args parameter now, this means that we can pass    # down parameters to the function being executed as a child process.    process  multiprocessing.Process(targetchild_process, args(queue,1, 2))    # We then start the process    process.start()    # And finally, we join the process. This will make our script to hang and    # wait until the child process is done.    process.join()    # Accessing the result from the queue.    print(fGot the result from child process as {queue.get()}) 它将给出以下输出 [r0x0dfedora ~]$ python /tmp/tmp.iuW2VAurGG/scratch.pyHi! Im process 149002Hi! Im a child process 149003. I do calculations.Got the result from child process as 3 异常处理 处理异常是一项特殊且有些困难的任务我们在使用流程模块时必须不时地完成它。原因是默认情况下子进程内发生的任何异常将始终由生成它的 Process 类处理。 下面的代码引发带有文本的异常 #!/usr/bin/env pythonimport osimport multiprocessingdef child_process():    print(fHi! Im a child process {os.getpid()}.)    raise Exception(Oh no! :()if __name__  __main__:    print(fHi! Im process {os.getpid()})    # Here we create a new instance of the Process class and assign our    # child_process function to be executed. Note the difference now that    # we are using the args parameter now, this means that we can pass    # down parameters to the function being executed as a child process.    process  multiprocessing.Process(targetchild_process)    try:        # We then start the process        process.start()        # And finally, we join the process. This will make our script to hang and        # wait until the child process is done.        process.join()        print(AFTER CHILD EXECUTION! RIGHT?!)    except Exception:        print(Uhhh... It failed?) 输出结果 [r0x0dfedora ~]$ python /tmp/tmp.iuW2VAurGG/scratch.pyHi! Im process 149505Hi! Im a child process 149506.Process Process-1:Traceback (most recent call last):  File /usr/lib64/python3.11/multiprocessing/process.py, line 314, in _bootstrap    self.run()  File /usr/lib64/python3.11/multiprocessing/process.py, line 108, in run    self._target(*self._args, **self._kwargs)  File /tmp/tmp.iuW2VAurGG/scratch.py, line 7, in child_process    raise Exception(Oh no! :()Exception: Oh no! :(AFTER CHILD EXECUTION! RIGHT?! 如果您跟踪代码您将能够注意到在 process.join() 调用之后仔细放置了一条 print 语句以模拟父进程仍在运行即使在子进程中引发了未处理的异常之后也是如此。 克服这种情况的一种方法是在子进程中实际处理异常如下所示 #!/usr/bin/env pythonimport osimport multiprocessingdef child_process():    try:        print(fHi! Im a child process {os.getpid()}.)        raise Exception(Oh no! :()    except Exception:        print(Uh, I think its fine now...)if __name__  __main__:    print(fHi! Im process {os.getpid()})    # Here we create a new instance of the Process class and assign our    # child_process function to be executed. Note the difference now that    # we are using the args parameter now, this means that we can pass    # down parameters to the function being executed as a child process.    process  multiprocessing.Process(targetchild_process)    # We then start the process    process.start()    # And finally, we join the process. This will make our script to hang and    # wait until the child process is done.    process.join()    print(AFTER CHILD EXECUTION! RIGHT?!) 现在您的异常将在您的子进程内处理这意味着您可以控制它会发生什么以及在这种情况下应该做什么。 总结 当工作和实现依赖于并行方式执行的解决方案时多处理模块非常强大特别是与 Process 类一起使用时。这增加了在其自己的隔离进程中执行任何函数的惊人可能性。 Reference [1] Source: https://developers.redhat.com/articles/2023/07/27/how-use-python-multiprocessing-module 本文由 mdnice 多平台发布
http://www.ihoyoo.com/news/32263.html

相关文章:

  • 知名网站建设在哪里顺平网站建设
  • sns网站设计网站建设公司专业网站开发需求
  • dw软件可以做哪些网站好的建设网站公司哪家好
  • 为什么网站有不同的扩展名wordpress 云储存
  • 网站建设的意见个人作品网站
  • 品牌网站建设推广想做苗木生意网站怎么怎么做
  • 网站加速网站推广的方式?
  • 模版网站可以做排名嘛网站表单提交到qq邮箱
  • 揭阳网站制作多少钱seo网站优化推广怎么样
  • vue适合什么样的网站开发河南省住房和城乡建设厅官方网站
  • 网站建设的毕业设计ppt设计师兼职
  • sns社交网站注册网站建设期末作业
  • 进入官方网站wordpress google推广
  • 建设中学校园网站的目的网站代码优化视频教程
  • 怎么做五合一网站WordPress分类ID能修改吗
  • 精美的微网站php租车网站
  • 西部数码网站站点成都建设银行招聘网站
  • 四川省住房和城乡建设厅网站电话微信版本的wordpress
  • 学做网站开发要1万6怎么做网站页面免费的
  • 茂名市网站建设网站左侧漂浮导航
  • h5响应式网站建设报价wordpress子主题视频
  • 杭州建站怎么给公司做简单网站
  • 单位网站备案要等多久2345传奇世界游戏介绍
  • 什么是网站风格excel 表格 做的网站
  • 特效视频网站网站建设方案 预算
  • 学院网站群建设的目标怎么制作网站一键更新
  • 每年网站备案抽查网页版登录入口
  • 东莞企业建站程序中国摄影展览网首页
  • 做网站主机手机网站建设需求
  • 购物网站开发分工网络规划设计师教程第二版