Title: Return results from asyncio.get_event_loop
Tags: python-3.x;python-requests;python-asyncio
Question: 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))
```


Here is the accepted answer: 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)).
```



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:05/07/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python-3.x><python-requests><python-asyncio>', 'Title': 'Return results from asyncio.get_event_loop', 'ViewCount': 1179, 'id': 50220663, 'user_AboutMe': None, 'user_AccountId': 6934237.0, 'user_DisplayName': 'J. Skinner', 'user_Reputation': 169.0, 'user_Views': 56.0, 'user_id': 5322745.0}}