python / cpython

The Python programming language
https://www.python.org
Other
63.67k stars 30.51k forks source link

Documentation: Clarify role of callback with map_async #73294

Open 9b09a310-ddaf-41a5-8429-1320ddbfa230 opened 7 years ago

9b09a310-ddaf-41a5-8429-1320ddbfa230 commented 7 years ago
BPO 29108
Nosy @applio
Files
  • main2.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['type-bug', 'docs'] title = 'Documentation: Clarify role of callback with map_async' updated_at = user = 'https://bugs.python.org/JoseMiguelColella' ``` bugs.python.org fields: ```python activity = actor = 'iritkatriel' assignee = 'docs@python' closed = False closed_date = None closer = None components = ['Documentation'] creation = creator = 'Jose Miguel Colella' dependencies = [] files = ['46084'] hgrepos = [] issue_num = 29108 keywords = [] message_count = 4.0 messages = ['284295', '284296', '284305', '284320'] nosy_count = 3.0 nosy_names = ['docs@python', 'davin', 'Jose Miguel Colella'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue29108' versions = ['Python 3.6'] ```

    9b09a310-ddaf-41a5-8429-1320ddbfa230 commented 7 years ago

    Hello I am trying to use the callback for the map_async method for Pool, but have found a bug. In the below code, only the print statement is carried out, the return is completely ignored. Is this working as designed or is this a bug?

    from multiprocessing import Pool
    
    def f(x):
        return x * x
    
    def s(x):
        print(f'Here: {x}')
        return type(x)
    
    if __name__ == '__main__':
        with Pool(5) as p:
            result = p.map_async(f, [1, 2, 3], callback=s)
            q = result.get()
            print(q)
    9b09a310-ddaf-41a5-8429-1320ddbfa230 commented 7 years ago

    The result is: Here: [1, 4, 9] [1, 4, 9]

    applio commented 7 years ago

    This appears to be working as designed. The purpose of the callback is to receive a single argument (nominally, the ready result from the map operation) and perform its task (whatever it may be) very quickly so as not to further delay the handing back of results from the map operation.

    Attempting to read into your comments, it sounds like you were perhaps expecting the return from the callback to appear somewhere? If that was your expectation then no, that is not the purpose of a callback here. Any transformation of input data to the final output data is performed by the function being applied by the map. While there are many potential uses for a callback, its stated requirement is that it be as quick an operation as possible as opposed to something that further transforms the results or anything like a "reduce" operation which is not necessarily quick.

    If you think this needs clarification in the documentation, please suggest something?

    9b09a310-ddaf-41a5-8429-1320ddbfa230 commented 7 years ago

    Hello David, Thanks for your response. Improvements to the documentation could clear this misunderstanding. I had initially believed that after transforming with the function passed to the map, it would use the callback on each of the result arguments. Just to understand the use case of the callback. So basically it should not return anything and be a simple print?