0

I'm using multiprocessing library (not new in python but new in multiprocessing). It seems that I lack of understanding how it works.

What I try to do: I send a lot of http requests to server and if I receive connection error it means that remote service is down and I restart it using paramiko and then resend a request. I use multiprocessing to load all available processors because there are about 70000 requests and it takes about 24 hours to process them all using one processor.

My code:

# Send request here
def send_request(server, url, data, timeout):
try:
    return requests.post(server + url, json=data, timeout=(timeout or 60))
except Exception:
    return None

# Try to get json from response
def do_gw_requests(request, data):
    timeout = 0
    response = send_request(server, request, data, timeout)
    if response is not None:
        response_json = json.loads(response.text)
    else:
        response_json = None
    return response_json

# Function that recall itself if service is down
def safe_build(data):
exception_message = ""
response = {}
try:
    response = do_gw_requests("/rgw_find_route", data)
    if response is None:
        # Function that uses paramiko to start service
        # It will not end until service is up
        start_service()
        while response is None:
            safe_build(data)
    --some other work here--
return response, exception_message

# Multiprocessing lines in main function
pool = Pool(2)
# build_single_route prepares data, calls safe_build once and write logs
result = pool.map_async(build_single_route, args)
pool.close()
pool.join()

My problem is if service already down at the start of script (and potentially if service got down in the middle of script's work) I can't get non-empty response for two first requests. Script starts, send two first requests (I send them in loop by two), finds out that service is down (response become None), restarts service, resends requests and seems gets None again and again and again (in endless loop). If I remove loop while response is None: then first two requests will process as if they was None and other requests will process as expected. But I need every request result that's why I resend bad requests. So it recall function with same data again and again but without success. It's very strange as for me. Can anyone please explain what am I doing wrong here?

selotec
  • 330
  • 3
  • 10

1 Answers1

0

It seems that problem not with behavior of Pool workers as I expected. response is a local variable of function and thus it become not None after reviving of service at the second call of safe_build, it's still None in the first call. response, _ = safe_build(data) seems work.

selotec
  • 330
  • 3
  • 10
  • Should add that later I refused of recursion because of recursion limit - http://stackoverflow.com/questions/3323001/maximum-recursion-depth – selotec May 13 '16 at 09:57