| 151 | |
| 152 | |
| 153 | def run_sync(func): |
| 154 | global separate_io_loop |
| 155 | |
| 156 | if separate_io_loop is None: |
| 157 | with io_loop_lock: |
| 158 | if separate_io_loop is None: |
| 159 | io_loop_future = Future() |
| 160 | |
| 161 | def run_new_ioloop(): |
| 162 | try: |
| 163 | io_loop = asyncio.new_event_loop() |
| 164 | io_loop_future.set_result(io_loop) |
| 165 | io_loop.run_forever() |
| 166 | except Exception as e: |
| 167 | io_loop_future.set_exception(e) |
| 168 | |
| 169 | # We need to run it in another thread because ioloop.current.run_sync starts/stops the current ioloop, |
| 170 | # which is not acceptable |
| 171 | thread = threading.Thread(target=run_new_ioloop, daemon=True) |
| 172 | thread.start() |
| 173 | separate_io_loop = io_loop_future.result() |
| 174 | |
| 175 | future = asyncio.run_coroutine_threadsafe(func, separate_io_loop) |
| 176 | return future.result() |