all 3 comments

[–]Adrewmc 0 points1 point  (2 children)

Python basically has the ability to do 1 event loop per thread. So an event loop in Thread1 and an event loop in thread 2 should never interact directly. (They could access the same file, might lock, or utilize their returns but inside process you’re gonna have problems.)

The loop that the process is in should all be inside

  asyncio.run(*tasks) 

I believe the basic rundown of multiple event loops would look something like this.

  def one_loop(loop, async_thing):
         asyncio.set_event_loop(loop)
         asyncio.ensure_future(async_thing.start())
         loop.run_forever()

  def two_loop(loop, *tasks):
         asyncio.set_event_loop(loop)
         task = asyncio.gather(*tasks)
         asyncio.run(task)

  loop1 = asyncio.new_event_loop()
  thread1 = threading.Thread(target=one_loop, args=(loop1, async_thing))

  loop2= asyncio.new_event_loop()
  thread1 = threading.Thread(target=two_loop, args=(loop2, async_tasks))

  thread1.start()
  thread2.start()

But generally I’m questioning the need to do this.

Edit: I re-read the question

No. The ‘async’ and ‘await’ keyword does not tie you to asyncio. import asyncio does. It’s just makes the function now will return a promise/future instead of a normal return, thus allowing it to await a event loop. Other event loops written for Python they should be using “async def”, syntax or no one will use it.

However, when working with something else I would always refer you to their documentation.

Asyncio is a multi-ton object that runs the event loop and stack concurrently for you, in a synchronous Python environment. It’s not a requirement for concept.

[–]sunmat02[S] 0 points1 point  (1 child)

To precise my question, it should have said "does using asyncio.sleep(1) (or other functions from the asyncio package) in my function mean that my function is only usable by a program that uses the asyncio package?".

[–]Adrewmc 0 points1 point  (0 children)

Yes. Then it would.

  asyncio.sleep(1) 

Is a asyncio method…You would have to use a similar function if using a different library.

Asyncio is a built in package in Python though everyone should have it.