0

I'm new to using the asyncio module. I have the following code that is querying a service to return IDs. How do I set a variable to return the results from the 'findIntersectingFeatures' function?

Also, how can I have the print statements execute after run_in_executor has finished. They are currently printing immediately after the first iteration.

import json, requests, time
import asyncio

startTime = time.clock()

out_json = "UML10kmbuffer.json"

intersections = []

def findIntersectingFeatures(coordinate):

    coordinates = '{"rings":' + str(coordinate) + '}'
    forestCoverURL = 'http://server1.ags.com/server/rest/services/Forest_Cover/MapServer/0/query'
    params = {'f': 'json', 'where': "1=1", 'outFields': '*', 'geometry': coordinates, 'geometryType': 'esriGeometryPolygon', 'returnIdsOnly': 'true'}
    r = requests.post(forestCoverURL, data = params, verify=False)
    response = json.loads(r.content)

    if response['objectIds'] != None:
        intersections.append(response['objectIds'])

    return intersections


with open(out_json, "r") as f_in:
    for line in f_in:
        json_res = json.loads(line)

coordinates = []

# Get features
feat_json = json_res["features"]
for item in feat_json:
    coordinates.append(item["geometry"]["rings"])

loop = asyncio.get_event_loop()

for coordinate in coordinates:
    loop.run_in_executor(None, findIntersectingFeatures, coordinate)


print("Intersecting Features:  " + str(intersections))

endTime = time.clock()
elapsedTime =(endTime - startTime) / 60
print("Elapsed Time: " + str(elapsedTime))
J. Skinner
  • 309
  • 1
  • 8
  • 17

1 Answers1

2

To use asyncio, you shouldn't just get the event loop, you must also run it. You can use run_until_complete to run a coroutine to completion. Since you need to run many coroutines in parallel, you can use asyncio.gather to combine them into a single parallel task:

coroutines = []
for coordinate in coordinates:
    coroutines.append(loop.run_in_executor(
        None, findIntersectingFeatures, coordinate))

intersections = loop.run_until_complete(asyncio.gather(*coroutines))

Also, how can I have the print statements execute after run_in_executor has finished.

You can await the call to run_in_executor and place your print after it:

def find_features(coordinate):
    inter = await loop.run_in_executor(None, findInterestingFeatures, coordinate)
    print('found', inter)
    return inter

# in the for loop, replace coroutines.append(loop.run_in_executor(...))
# with coroutines.append(find_features(coordinate)).
user4815162342
  • 141,790
  • 18
  • 296
  • 355