banteg / multicall.py

aggregate results of multiple smart contract calls into one
MIT License
251 stars 108 forks source link

Don't work with multi threads and jupyter #41

Closed lucasccampos closed 1 year ago

lucasccampos commented 2 years ago

running Multicall in main thread works great, but running the function twice in another thread is going to throw an error

"RuntimeError: Timeout context manager should be used inside a task"

and when using Multicall with jupyter, u get this error

File ~/.local/lib/python3.10/site-packages/multicall/multicall.py:57, in Multicall.call(self) 55 def call(self) -> Dict[str,Any]: 56 start = time() ---> 57 response = await_awaitable(self.coroutine()) 58 logger.debug(f"Multicall took {time() - start}s") 59 return response

File ~/.local/lib/python3.10/site-packages/multicall/utils.py:67, in await_awaitable(awaitable) 66 def await_awaitable(awaitable: Awaitable) -> Any: ---> 67 return get_event_loop().run_until_complete(awaitable)

File /usr/lib64/python3.10/asyncio/base_events.py:622, in BaseEventLoop.run_until_complete(self, future) 611 """Run until the Future is done. 612 613 If the argument is a coroutine, it is wrapped in a Task. (...) 619 Return the Future's result, or raise its exception. 620 """ 621 self._check_closed() --> 622 self._check_running() 624 new_task = not futures.isfuture(future) 625 future = tasks.ensure_future(future, loop=self)

File /usr/lib64/python3.10/asyncio/base_events.py:582, in BaseEventLoop._check_running(self) 580 def _check_running(self): 581 if self.is_running(): --> 582 raise RuntimeError('This event loop is already running') 583 if events._get_running_loop() is not None: 584 raise RuntimeError( 585 'Cannot run the event loop while another loop is running')

RuntimeError: This event loop is already running

cheekybastard commented 2 years ago

latest multicall works with jupyter if you install: https://github.com/erdewit/nest_asyncio

in the jupyter cell add:

import nest_asyncio nest_asyncio.apply()

BobTheBuidler commented 1 year ago

multithread issue is fixed if you use web3>=5.31.3

Jupyter issue is related to jupyter event loop and solution has been shared above. Closing.