1

I'm trying to gather data from a single url using python requests library.

I want to run multiprocessing to speed up the data collection however I get an error when I pass in the argument of my function inside of Pool.

Kindly note I have already read the following previous questions:

a link and a link however none of those answer my question.

How can I run those get request simultaneously passing the 3 mandatory arguments?

Here is my code:

from multiprocessing import Pool
import requests
url = 'http://icanhazip.com'
url_two = 'https://httpbin.org/ip'
url_three = 'https://httpbin.org/get'
start_point = 'a'
start_point_two = 'b'
start_point_three ='c'
ending_point = 'c'
ending_point_two = 'z'
ending_point_three = 'x'


def get_info(url,start_point,ending_point):
    r = requests.get(url)
    html = r.text
    if start_point in html:
        print('Do Something')
    elif ending_point in html:
        print('Do Something else')
   else:
        pass

if __name__ == '__main__':
    with Pool(5) as p:
        print(p.map(get_info, [[url,start_point,ending_point]]))

This is the error I get:

TypeError: get_info() missing 2 required positional arguments: 'start_point' and 'ending_point'
1
  • use one argument get_info(args) and upack it inside get_info - url,start_point,ending_point = args Commented Jul 21, 2019 at 17:40

1 Answer 1

1

To pass multiple arguments to a target function - use Pool.starmap feature:

In your case it'd look as below:

if __name__ == '__main__':
    with Pool(5) as p:
        print(p.starmap(get_info, [(url, start_point, ending_point),
                               (url_two, start_point_two, ending_point_two),
                               (url_three, start_point_three, ending_point_three),]
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @RomanPerekhrest, it worked with the .starmap suggestion you gave, can you please modify the answer replacing ("map" with "starmap" after print) so that I can assign you the answer and people reading it in future will have the solution ready and correct? Thanks
@SkylerX, sorry, sure. That was a typo, fixed now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.