Multi-Process---Error resolution attribute error

Source: Internet
Author: User
Tags function definition
Multi-Process---Error resolution attribute Error:can ' t get attributes ' Long_time_task ' on <module ' __main__ ' (built-in) >

StackOverflow Solutions

This problem seems to be a design feature of multiprocessor. Please refer to https://bugs.python.org/issue25053. Pool cannot always use objects that are not defined in the imported module for some reason. So you have to write the function to a different file and import the module.

Solutions
One file generates an error, the function definition is placed in another py file and then introduced
one.py

From multiprocessing import Pool
import long_time_task
import os

if __name__== ' __main__ ':
    print (' Parent process%s. '% Os.getpid ())
    p = Pool (4)
    for I in range (5):
        P.apply_async (Long_time_task, args= (i,)) C8/>print (' Waiting for all subprocesses done ... ')
    p.close () p.join ()
    print (' All subprocesses done. ')

long_time_task.py

import os, Time, Random def long_time_task (name): Print (' Run task%s (%s) ... '% (name, os.ge Tpid ())) start = Time.time () time.sleep (Random.random () * 3) end = Time.time () print (' Task%s runs%0.2f s Econds. '% (name, (End-start)) 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.