I have a list of 155 host + databases and I want to create a simple app in jupyter notebook that runs some query on them. Example looks as follows: SQL = """ SELECT 1 """ import asyncio import aiopg import nest_asyncio nest_asyncio.apply() loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) async def runQuery(host, db, sql): print(str(host), str(db)) if str(host) == 'nan': return 'No host' try: conn = await aiopg.connect(database=str(db), user='some_user', password='some_password', host=str(host), timeout = 2) print('xxxx') except (psycopg2.Error, asyncio.TimeoutError, psycopg2.OperationalError) as e: return 'Error creating connection exception' if not conn or conn.closed == 1: return 'Error creating connection' try: cur = await conn.cursor() if not cur: return 'No cursor' await cur.execute(sql) result = await cur.fetchall() await conn.close() return result except psycopg2.Error as e: await conn.close() return 'Error fetching' async def main(): tasks = [] for item in csvFile: tasks.extend([runQuery(item['host'], it, SQL) for it in item['dbs']) results = [] i = 90 # to start with host+db number 90 d = 1 # how many to run at once while i <= len(tasks): r = await asyncio.gather(*tasks[ii+d)]) i = i+d print(i) results.extend(r) return results results = loop.run_until_complete(main()) print(results) loop.close() I have a very weird problem, where everything works fine but not with several combinations of host+database. I have in list 155 combinations, and it fails at number 90, and then 120 which looks completely correct. The problem is, I can't catch this error since it is not throwing any exception in the function runQuery, line conn=await aiopg.connect... (and I don't want to abort whole process, just this connection) even though I know it happens during this call. Any idea what I can do to catch the exception and ignore it only for these single combinations of host and database? Or deal with it other way? I am getting error: OSError: [WinError 10038] An operation was attempted on something that is not a socket Full traceback: --------------------------------------------------------------------------- OSError Traceback (most recent call last) Cell In[9], line 70 68 #results = await asyncio.gather(*tasks) 69 return results ---> 70 results = loop.run_until_complete(main()) 71 print(results) 73 loop.close() File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\nest_asyncio.py:92, in _patch_loop.<locals>.run_until_complete(self, future) 90 f._log_destroy_pending = False 91 while not f.done(): ---> 92 self._run_once() 93 if self._stopping: 94 break File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\nest_asyncio.py:115, in _patch_loop.<locals>._run_once(self) 108 heappop(scheduled) 110 timeout = ( 111 0 if ready or self._stopping 112 else min(max( 113 scheduled[0]._when - self.time(), 0), 86400) if scheduled 114 else None) --> 115 event_list = self._selector.select(timeout) 116 self._process_events(event_list) 118 end_time = self.time() + self._clock_resolution File ~\AppData\Local\Programs\Python\Python312\Lib\selectors.py:323, in SelectSelector.select(self, timeout) 321 ready = [] 322 try: --> 323 r, w, _ = self._select(self._readers, self._writers, [], timeout) 324 except InterruptedError: 325 return ready File ~\AppData\Local\Programs\Python\Python312\Lib\selectors.py:314, in SelectSelector._select(self, r, w, _, timeout) 313 def _select(self, r, w, _, timeout=None): --> 314 r, w, x = select.select(r, w, w, timeout) 315 return r, w + x, [] OSError: [WinError 10038] An operation was attempted on something that is not a socket Continue reading...