asyncio producer consumer calling from non async main thread









up vote
0
down vote

favorite












I'm trying to run asyncio in a Thread. The reason I'm doing this is, that I want to communicate with asyncio from a non async main thread.
So here is my code



import asyncio
import threading

async def consume(input_q, output_q):
while True:
item = await input_q.get()
print(item)
output_q.put("hello back")

async def run(input_q, output_q):
asyncio.ensure_future(consume(input_q, output_q))
while True:
await asyncio.sleep(1)
print("message")

def run_in_thread(loop,input_q,output_q):
asyncio.set_event_loop(loop)
loop.run_until_complete(run(input_q, output_q))
loop = asyncio.new_event_loop()
input_q = asyncio.Queue(loop=loop)
output_q = asyncio.Queue(loop=loop)
thread = threading.Thread(target=run_in_thread ,args=(loop, input_q, output_q))
thread.start()
time.sleep(5)
asyncio.run_coroutine_threadsafe(input_q.put("hello input"), loop)
time.sleep(2)
item = asyncio.run_coroutine_threadsafe(output_q.get_nowait(), loop).result()
assert "hello back" in item
print(item)
loop.call_soon_threadsafe(loop.stop)
thread.join()


I'm able to put messages to the input queue from the main thread. Bun unfortunately not able to read from the output_q. Is there any solution for this?
Getting the error on this line:



item = asyncio.run_coroutine_threadsafe(output_q.get_nowait(), loop).result() 
--> raises QueueEmpty


Maybe using multiprocessing or whatever. What I would like to do is run the above functions and communicate from another process or thread with them which doesn't use async. Thanks a lot for any hint!



PS: I'm using python 3.5










share|improve this question

























    up vote
    0
    down vote

    favorite












    I'm trying to run asyncio in a Thread. The reason I'm doing this is, that I want to communicate with asyncio from a non async main thread.
    So here is my code



    import asyncio
    import threading

    async def consume(input_q, output_q):
    while True:
    item = await input_q.get()
    print(item)
    output_q.put("hello back")

    async def run(input_q, output_q):
    asyncio.ensure_future(consume(input_q, output_q))
    while True:
    await asyncio.sleep(1)
    print("message")

    def run_in_thread(loop,input_q,output_q):
    asyncio.set_event_loop(loop)
    loop.run_until_complete(run(input_q, output_q))
    loop = asyncio.new_event_loop()
    input_q = asyncio.Queue(loop=loop)
    output_q = asyncio.Queue(loop=loop)
    thread = threading.Thread(target=run_in_thread ,args=(loop, input_q, output_q))
    thread.start()
    time.sleep(5)
    asyncio.run_coroutine_threadsafe(input_q.put("hello input"), loop)
    time.sleep(2)
    item = asyncio.run_coroutine_threadsafe(output_q.get_nowait(), loop).result()
    assert "hello back" in item
    print(item)
    loop.call_soon_threadsafe(loop.stop)
    thread.join()


    I'm able to put messages to the input queue from the main thread. Bun unfortunately not able to read from the output_q. Is there any solution for this?
    Getting the error on this line:



    item = asyncio.run_coroutine_threadsafe(output_q.get_nowait(), loop).result() 
    --> raises QueueEmpty


    Maybe using multiprocessing or whatever. What I would like to do is run the above functions and communicate from another process or thread with them which doesn't use async. Thanks a lot for any hint!



    PS: I'm using python 3.5










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to run asyncio in a Thread. The reason I'm doing this is, that I want to communicate with asyncio from a non async main thread.
      So here is my code



      import asyncio
      import threading

      async def consume(input_q, output_q):
      while True:
      item = await input_q.get()
      print(item)
      output_q.put("hello back")

      async def run(input_q, output_q):
      asyncio.ensure_future(consume(input_q, output_q))
      while True:
      await asyncio.sleep(1)
      print("message")

      def run_in_thread(loop,input_q,output_q):
      asyncio.set_event_loop(loop)
      loop.run_until_complete(run(input_q, output_q))
      loop = asyncio.new_event_loop()
      input_q = asyncio.Queue(loop=loop)
      output_q = asyncio.Queue(loop=loop)
      thread = threading.Thread(target=run_in_thread ,args=(loop, input_q, output_q))
      thread.start()
      time.sleep(5)
      asyncio.run_coroutine_threadsafe(input_q.put("hello input"), loop)
      time.sleep(2)
      item = asyncio.run_coroutine_threadsafe(output_q.get_nowait(), loop).result()
      assert "hello back" in item
      print(item)
      loop.call_soon_threadsafe(loop.stop)
      thread.join()


      I'm able to put messages to the input queue from the main thread. Bun unfortunately not able to read from the output_q. Is there any solution for this?
      Getting the error on this line:



      item = asyncio.run_coroutine_threadsafe(output_q.get_nowait(), loop).result() 
      --> raises QueueEmpty


      Maybe using multiprocessing or whatever. What I would like to do is run the above functions and communicate from another process or thread with them which doesn't use async. Thanks a lot for any hint!



      PS: I'm using python 3.5










      share|improve this question













      I'm trying to run asyncio in a Thread. The reason I'm doing this is, that I want to communicate with asyncio from a non async main thread.
      So here is my code



      import asyncio
      import threading

      async def consume(input_q, output_q):
      while True:
      item = await input_q.get()
      print(item)
      output_q.put("hello back")

      async def run(input_q, output_q):
      asyncio.ensure_future(consume(input_q, output_q))
      while True:
      await asyncio.sleep(1)
      print("message")

      def run_in_thread(loop,input_q,output_q):
      asyncio.set_event_loop(loop)
      loop.run_until_complete(run(input_q, output_q))
      loop = asyncio.new_event_loop()
      input_q = asyncio.Queue(loop=loop)
      output_q = asyncio.Queue(loop=loop)
      thread = threading.Thread(target=run_in_thread ,args=(loop, input_q, output_q))
      thread.start()
      time.sleep(5)
      asyncio.run_coroutine_threadsafe(input_q.put("hello input"), loop)
      time.sleep(2)
      item = asyncio.run_coroutine_threadsafe(output_q.get_nowait(), loop).result()
      assert "hello back" in item
      print(item)
      loop.call_soon_threadsafe(loop.stop)
      thread.join()


      I'm able to put messages to the input queue from the main thread. Bun unfortunately not able to read from the output_q. Is there any solution for this?
      Getting the error on this line:



      item = asyncio.run_coroutine_threadsafe(output_q.get_nowait(), loop).result() 
      --> raises QueueEmpty


      Maybe using multiprocessing or whatever. What I would like to do is run the above functions and communicate from another process or thread with them which doesn't use async. Thanks a lot for any hint!



      PS: I'm using python 3.5







      python-3.x multithreading pytest-asyncio






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 21:53









      MichaelRazum

      15410




      15410



























          active

          oldest

          votes











          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%2f53233713%2fasyncio-producer-consumer-calling-from-non-async-main-thread%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233713%2fasyncio-producer-consumer-calling-from-non-async-main-thread%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo