omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Asyncio doesn't work in pythonista 3

    Pythonista
    asyncio
    4
    8
    9012
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • jeremyherbert
      jeremyherbert last edited by ccc

      Hi,

      I've just purchased pythonista 3 and I'm trying to run the following code straight from the docs:

      import asyncio
      
      async def hello_world():
          print("Hello World!")
      
      loop = asyncio.get_event_loop()
      # Blocking call which returns when the hello_world() coroutine is done
      loop.run_until_complete(hello_world())
      loop.close()
      

      I get the error: RuntimeError: There is no current event loop in thread 'Dummy-1'.

      Am I doing something wrong?

      Thanks,
      Jeremy

      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by ccc

        Asyncio does work in Pythonista. If you double click the home button and throw Pythonista 3 out of memory and then relaunch Pythonista 3 and rerun your script, it will work.

        My advise is that you comment out the line loop.close() and then you will be able to run your asyncio script multiple times in a single Pythonista session.

        Added: https://github.com/omz/Pythonista-Issues/issues/43

        1 Reply Last reply Reply Quote 0
        • jeremyherbert
          jeremyherbert last edited by

          Great, that workaround does sort it out in the meantime. Thank you for reporting the bug for me :)

          1 Reply Last reply Reply Quote 0
          • JonB
            JonB last edited by

            loop = asyncio.get_event_loop_policy().new_event_loop()
            
            mikael 1 Reply Last reply Reply Quote 0
            • mikael
              mikael @JonB last edited by

              I am confused. The issue was closed as ”fixed”, while all examples everywhere use get_event_loop and fail with the error stating There is no current event loop in thread 'Dummy-1'.

              Why is this not an issue with Pythonista?

              1 Reply Last reply Reply Quote 0
              • JonB
                JonB last edited by

                As I understand things, the code above does work, the first time it is run.

                But, because the pythonista environment is not fully reset when running a new script, the default event loop is now closed. This would be same as if you ran this code multiple times in IDLE for example, without python exiting. So bot so much a bug, as a subtlety with operating in a interactive environment. Other examples of things like this, include logging loggers dont get cleared out, which is a good thing sometimes but also mean your code needs to check for existing handlers before adding new ones.

                What users can do is use

                asyncio.set_event_loop(asyncio.new_event_loop())
                

                after loop.close, to reset the default event loop. Then get_event_loop works again. In theory, this could go into pythonista_preflight.py, however it is not obvious to me this wouldnt cause other issues, or would really be the intended behavior.

                mikael 2 Replies Last reply Reply Quote 1
                • mikael
                  mikael @JonB last edited by

                  @JonB, thanks, that clarifies things.

                  1 Reply Last reply Reply Quote 0
                  • mikael
                    mikael @JonB last edited by

                    @JonB, maybe some of these errors come from Pythonista running UI in separate thread, which does not know about the loop unless I provide it specifically. Need to experiment.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post
                    Powered by NodeBB Forums | Contributors