AttributeError: module 'asyncio' has no attribute 'create_task'









up vote
4
down vote

favorite












I'm trying to asyncio.create_task() but I dealing with this error:



Here's an example:



import asyncio
import time

async def async_say(delay, msg):
await asyncio.sleep(delay)
print(msg)

async def main():
task1 = asyncio.create_task(async_say(4, 'hello'))
task2 = asyncio.create_task(async_say(6, 'world'))

print(f"started at time.strftime('%X')")
await task1
await task2
print(f"finished at time.strftime('%X')")

loop = asyncio.get_event_loop()
loop.run_until_complete(main())


Out:



AttributeError: module 'asyncio' has no attribute 'create_task'



So I tried with the following snippet code (.ensure_future()) instead, without any problem:



async def main():
task1 = asyncio.ensure_future(async_say(4, 'hello'))
task2 = asyncio.ensure_future(async_say(6, 'world'))

print(f"started at time.strftime('%X')")
await task1
await task2
print(f"finished at time.strftime('%X')")

loop = asyncio.get_event_loop()
loop.run_until_complete(main())


Out:



started at 13:19:44
hello
world
finished at 13:19:50



[UPDATE]:



With borrowing from @user4815162342 answer, I updated my question:



async def main():
loop = asyncio.get_event_loop()
task1 = loop.create_task(async_say(4, 'hello'))
task2 = loop.create_task(async_say(6, 'world'))

print(f"started at time.strftime('%X')")
await task1
await task2
print(f"finished at time.strftime('%X')")

loop = asyncio.get_event_loop()
loop.run_until_complete(main())



What's wrong?




[NOTE]:



  • Python 3.6

  • Ubuntu 16.04









share|improve this question



























    up vote
    4
    down vote

    favorite












    I'm trying to asyncio.create_task() but I dealing with this error:



    Here's an example:



    import asyncio
    import time

    async def async_say(delay, msg):
    await asyncio.sleep(delay)
    print(msg)

    async def main():
    task1 = asyncio.create_task(async_say(4, 'hello'))
    task2 = asyncio.create_task(async_say(6, 'world'))

    print(f"started at time.strftime('%X')")
    await task1
    await task2
    print(f"finished at time.strftime('%X')")

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())


    Out:



    AttributeError: module 'asyncio' has no attribute 'create_task'



    So I tried with the following snippet code (.ensure_future()) instead, without any problem:



    async def main():
    task1 = asyncio.ensure_future(async_say(4, 'hello'))
    task2 = asyncio.ensure_future(async_say(6, 'world'))

    print(f"started at time.strftime('%X')")
    await task1
    await task2
    print(f"finished at time.strftime('%X')")

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())


    Out:



    started at 13:19:44
    hello
    world
    finished at 13:19:50



    [UPDATE]:



    With borrowing from @user4815162342 answer, I updated my question:



    async def main():
    loop = asyncio.get_event_loop()
    task1 = loop.create_task(async_say(4, 'hello'))
    task2 = loop.create_task(async_say(6, 'world'))

    print(f"started at time.strftime('%X')")
    await task1
    await task2
    print(f"finished at time.strftime('%X')")

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())



    What's wrong?




    [NOTE]:



    • Python 3.6

    • Ubuntu 16.04









    share|improve this question

























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I'm trying to asyncio.create_task() but I dealing with this error:



      Here's an example:



      import asyncio
      import time

      async def async_say(delay, msg):
      await asyncio.sleep(delay)
      print(msg)

      async def main():
      task1 = asyncio.create_task(async_say(4, 'hello'))
      task2 = asyncio.create_task(async_say(6, 'world'))

      print(f"started at time.strftime('%X')")
      await task1
      await task2
      print(f"finished at time.strftime('%X')")

      loop = asyncio.get_event_loop()
      loop.run_until_complete(main())


      Out:



      AttributeError: module 'asyncio' has no attribute 'create_task'



      So I tried with the following snippet code (.ensure_future()) instead, without any problem:



      async def main():
      task1 = asyncio.ensure_future(async_say(4, 'hello'))
      task2 = asyncio.ensure_future(async_say(6, 'world'))

      print(f"started at time.strftime('%X')")
      await task1
      await task2
      print(f"finished at time.strftime('%X')")

      loop = asyncio.get_event_loop()
      loop.run_until_complete(main())


      Out:



      started at 13:19:44
      hello
      world
      finished at 13:19:50



      [UPDATE]:



      With borrowing from @user4815162342 answer, I updated my question:



      async def main():
      loop = asyncio.get_event_loop()
      task1 = loop.create_task(async_say(4, 'hello'))
      task2 = loop.create_task(async_say(6, 'world'))

      print(f"started at time.strftime('%X')")
      await task1
      await task2
      print(f"finished at time.strftime('%X')")

      loop = asyncio.get_event_loop()
      loop.run_until_complete(main())



      What's wrong?




      [NOTE]:



      • Python 3.6

      • Ubuntu 16.04









      share|improve this question















      I'm trying to asyncio.create_task() but I dealing with this error:



      Here's an example:



      import asyncio
      import time

      async def async_say(delay, msg):
      await asyncio.sleep(delay)
      print(msg)

      async def main():
      task1 = asyncio.create_task(async_say(4, 'hello'))
      task2 = asyncio.create_task(async_say(6, 'world'))

      print(f"started at time.strftime('%X')")
      await task1
      await task2
      print(f"finished at time.strftime('%X')")

      loop = asyncio.get_event_loop()
      loop.run_until_complete(main())


      Out:



      AttributeError: module 'asyncio' has no attribute 'create_task'



      So I tried with the following snippet code (.ensure_future()) instead, without any problem:



      async def main():
      task1 = asyncio.ensure_future(async_say(4, 'hello'))
      task2 = asyncio.ensure_future(async_say(6, 'world'))

      print(f"started at time.strftime('%X')")
      await task1
      await task2
      print(f"finished at time.strftime('%X')")

      loop = asyncio.get_event_loop()
      loop.run_until_complete(main())


      Out:



      started at 13:19:44
      hello
      world
      finished at 13:19:50



      [UPDATE]:



      With borrowing from @user4815162342 answer, I updated my question:



      async def main():
      loop = asyncio.get_event_loop()
      task1 = loop.create_task(async_say(4, 'hello'))
      task2 = loop.create_task(async_say(6, 'world'))

      print(f"started at time.strftime('%X')")
      await task1
      await task2
      print(f"finished at time.strftime('%X')")

      loop = asyncio.get_event_loop()
      loop.run_until_complete(main())



      What's wrong?




      [NOTE]:



      • Python 3.6

      • Ubuntu 16.04






      python python-3.x async-await python-3.6 python-asyncio






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 15:03

























      asked Nov 11 at 9:50









      Benyamin Jafari

      2,43531833




      2,43531833






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          The create_task top-level function was added in Python 3.7, and you are using Python 3.6. Prior to 3.7, create_task was only available as a method on the event loop, so you can invoke it like that:



          async def main():
          loop = asyncio.get_event_loop()
          task1 = loop.create_task(async_say(4, 'hello'))
          task2 = loop.create_task(async_say(6, 'world'))


          That works in both 3.6 and 3.7, as well as in earlier versions. asyncio.ensure_future will work as well, but when you know you're dealing with a coroutine, create_task is more explicit and is the preferred option.






          share|improve this answer


















          • 1




            Thank's for the answer, but I encountered with this error: RuntimeWarning: coroutine 'main' was never awaited
            – Benyamin Jafari
            Nov 11 at 10:34






          • 1




            @BenyaminJafari Please edit the question to include the new code you are testing.
            – user4815162342
            Nov 11 at 11:26






          • 2




            I updated my question, problem solved +1
            – Benyamin Jafari
            Nov 11 at 12:59










          • @BenyaminJafari Thanks. Note that updating the question is not needed if the problem is solved.
            – user4815162342
            Nov 11 at 14:49










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247533%2fattributeerror-module-asyncio-has-no-attribute-create-task%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          The create_task top-level function was added in Python 3.7, and you are using Python 3.6. Prior to 3.7, create_task was only available as a method on the event loop, so you can invoke it like that:



          async def main():
          loop = asyncio.get_event_loop()
          task1 = loop.create_task(async_say(4, 'hello'))
          task2 = loop.create_task(async_say(6, 'world'))


          That works in both 3.6 and 3.7, as well as in earlier versions. asyncio.ensure_future will work as well, but when you know you're dealing with a coroutine, create_task is more explicit and is the preferred option.






          share|improve this answer


















          • 1




            Thank's for the answer, but I encountered with this error: RuntimeWarning: coroutine 'main' was never awaited
            – Benyamin Jafari
            Nov 11 at 10:34






          • 1




            @BenyaminJafari Please edit the question to include the new code you are testing.
            – user4815162342
            Nov 11 at 11:26






          • 2




            I updated my question, problem solved +1
            – Benyamin Jafari
            Nov 11 at 12:59










          • @BenyaminJafari Thanks. Note that updating the question is not needed if the problem is solved.
            – user4815162342
            Nov 11 at 14:49














          up vote
          3
          down vote



          accepted










          The create_task top-level function was added in Python 3.7, and you are using Python 3.6. Prior to 3.7, create_task was only available as a method on the event loop, so you can invoke it like that:



          async def main():
          loop = asyncio.get_event_loop()
          task1 = loop.create_task(async_say(4, 'hello'))
          task2 = loop.create_task(async_say(6, 'world'))


          That works in both 3.6 and 3.7, as well as in earlier versions. asyncio.ensure_future will work as well, but when you know you're dealing with a coroutine, create_task is more explicit and is the preferred option.






          share|improve this answer


















          • 1




            Thank's for the answer, but I encountered with this error: RuntimeWarning: coroutine 'main' was never awaited
            – Benyamin Jafari
            Nov 11 at 10:34






          • 1




            @BenyaminJafari Please edit the question to include the new code you are testing.
            – user4815162342
            Nov 11 at 11:26






          • 2




            I updated my question, problem solved +1
            – Benyamin Jafari
            Nov 11 at 12:59










          • @BenyaminJafari Thanks. Note that updating the question is not needed if the problem is solved.
            – user4815162342
            Nov 11 at 14:49












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          The create_task top-level function was added in Python 3.7, and you are using Python 3.6. Prior to 3.7, create_task was only available as a method on the event loop, so you can invoke it like that:



          async def main():
          loop = asyncio.get_event_loop()
          task1 = loop.create_task(async_say(4, 'hello'))
          task2 = loop.create_task(async_say(6, 'world'))


          That works in both 3.6 and 3.7, as well as in earlier versions. asyncio.ensure_future will work as well, but when you know you're dealing with a coroutine, create_task is more explicit and is the preferred option.






          share|improve this answer














          The create_task top-level function was added in Python 3.7, and you are using Python 3.6. Prior to 3.7, create_task was only available as a method on the event loop, so you can invoke it like that:



          async def main():
          loop = asyncio.get_event_loop()
          task1 = loop.create_task(async_say(4, 'hello'))
          task2 = loop.create_task(async_say(6, 'world'))


          That works in both 3.6 and 3.7, as well as in earlier versions. asyncio.ensure_future will work as well, but when you know you're dealing with a coroutine, create_task is more explicit and is the preferred option.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 19:25

























          answered Nov 11 at 10:05









          user4815162342

          59.2k488138




          59.2k488138







          • 1




            Thank's for the answer, but I encountered with this error: RuntimeWarning: coroutine 'main' was never awaited
            – Benyamin Jafari
            Nov 11 at 10:34






          • 1




            @BenyaminJafari Please edit the question to include the new code you are testing.
            – user4815162342
            Nov 11 at 11:26






          • 2




            I updated my question, problem solved +1
            – Benyamin Jafari
            Nov 11 at 12:59










          • @BenyaminJafari Thanks. Note that updating the question is not needed if the problem is solved.
            – user4815162342
            Nov 11 at 14:49












          • 1




            Thank's for the answer, but I encountered with this error: RuntimeWarning: coroutine 'main' was never awaited
            – Benyamin Jafari
            Nov 11 at 10:34






          • 1




            @BenyaminJafari Please edit the question to include the new code you are testing.
            – user4815162342
            Nov 11 at 11:26






          • 2




            I updated my question, problem solved +1
            – Benyamin Jafari
            Nov 11 at 12:59










          • @BenyaminJafari Thanks. Note that updating the question is not needed if the problem is solved.
            – user4815162342
            Nov 11 at 14:49







          1




          1




          Thank's for the answer, but I encountered with this error: RuntimeWarning: coroutine 'main' was never awaited
          – Benyamin Jafari
          Nov 11 at 10:34




          Thank's for the answer, but I encountered with this error: RuntimeWarning: coroutine 'main' was never awaited
          – Benyamin Jafari
          Nov 11 at 10:34




          1




          1




          @BenyaminJafari Please edit the question to include the new code you are testing.
          – user4815162342
          Nov 11 at 11:26




          @BenyaminJafari Please edit the question to include the new code you are testing.
          – user4815162342
          Nov 11 at 11:26




          2




          2




          I updated my question, problem solved +1
          – Benyamin Jafari
          Nov 11 at 12:59




          I updated my question, problem solved +1
          – Benyamin Jafari
          Nov 11 at 12:59












          @BenyaminJafari Thanks. Note that updating the question is not needed if the problem is solved.
          – user4815162342
          Nov 11 at 14:49




          @BenyaminJafari Thanks. Note that updating the question is not needed if the problem is solved.
          – user4815162342
          Nov 11 at 14:49

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247533%2fattributeerror-module-asyncio-has-no-attribute-create-task%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線