title: Why are all the tasks completed in asyncio.wait() when I clearly indicate that I want only the first one completed?
body: <p>I'm trying to fetch some data from OpenSubtitles using asyncio and then download a file who's information is contained in that data. I want to fetch that data and download the file at the same time using asyncio.</p>
  
  <p>The problem is that I want to wait for 1 task from the list <code>tasks</code> to finish before commencing with the rest of the tasks in the list or the <code>download_tasks</code>. The reason for this is that in <code>self._perform_query()</code> I am writing information to a file and in <code>self._download_and_save_file()</code> I am reading that same information from that file. So in other words, the <code>download_tasks</code> need to wait for at least one task in <code>tasks</code> to finish before starting.</p>
  
  <p>I found out I can do that with <code>asyncio.wait(return_when=FIRST_COMPLETED)</code> but for some reason it is not working properly:</p>
  
  <pre><code>payloads = [create_payloads(entry) for entry in retreive(table_in_database)] 
  tasks = [asyncio.create_task(self._perform_query(payload, proxy)) for payload in payloads]
  download_tasks = [asyncio.create_task(self._download_and_save_file(url, proxy) for url in url_list]
  
  done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
  print(done)
  print(len(done))
  print(pending)
  print(len(pending))
  await asyncio.wait(download_tasks)
  </code></pre>
  
  <p>The output is completely different than expected. It seems that out of 3 tasks in the list <code>tasks</code> all 3 of them are being completed despite me passing <code>asyncio.FIRST_COMPLETED</code>. Why is this happening?</p>
  
  <pre><code>{&lt;Task finished coro=&lt;SubtitleDownloader._perform_query() done, defined at C:\Users\...\subtitles.py:71&gt; result=None&gt;, &lt;Task finished coro=&lt;SubtitleDownloader._perform_query() done, defined at C:\Users\...\subtitles.py:71&gt; result=None&gt;, &lt;Task finished coro=&lt;SubtitleDownloader._perform_query() done, defined at C:\Users\...\subtitles.py:71&gt; result=None&gt;}
  3
  set()
  0
  Exiting
  </code></pre>
  
  <p>As far as I can tell, the code in <code>self._perform_query()</code> shouldn't affect this problem. Here it is anyway just to make sure:</p>
  
  <pre><code>async def _perform_query(self, payload, proxy):
      try:
          query_result = proxy.SearchSubtitles(self.opensubs_token, [payload], {"limit": 25})
      except Fault as e:
          raise "A fault has occurred:\n{}".format(e)
      except ProtocolError as e:
          raise "A ProtocolError has occurred:\n{}".format(e)
      else:
          if query_result["status"] == "200 OK":
              with open("dl_links.json", "w") as dl_links_json:
                  result = query_result["data"][0]
                  subtitle_name = result["SubFileName"]
                  download_link = result["SubDownloadLink"]
                  download_data = {"download link": download_link,
                                   "file name": subtitle_name}
                  json.dump(download_data, dl_links_json)
          else:
              print("Wrong status code: {}".format(query_result["status"]))
  </code></pre>
  
  <p>For now, I've been testing this without running <code>download_tasks</code> but I have included it here for context. Maybe I am going about this problem in a completely wrong manner. If so, I would much appreciate your input!</p>
  
  <p>Edit:</p>
  
  <p>The problem was very simple as answered below. <code>_perform_query</code> wasn't an awaitable function, instead it ran synchronously. I changed that by editing the file writing part of <code>_perform_query</code> to be asynchronous with <code>aiofiles</code>:</p>
  
  <pre><code>def _perform_query(self, payload, proxy):
      query_result = proxy.SearchSubtitles(self.opensubs_token, [payload], {"limit": 25})
      if query_result["status"] == "200 OK":
          async with aiofiles.open("dl_links.json", mode="w") as dl_links_json:
              result = query_result["data"][0]
              download_link = result["SubDownloadLink"]
              await dl_links_json.write(download_link)
  </code></pre>