aiohttp – different behaviour when aioprocessing is launched from view vs startup












0















I need to do some blocking cpu work, so I launch a new aioprocessing AioProcess and use an aioprocessing AioPipe to communicate with it.



launching the process looks like this:



async def launch_worker(app):
pipe_a, pipe_b = AioPipe()
process = AioProcess(target=worker, args=(pipe_b,))
process.start()
app.process, app.pipe = process, pipe_a
await sleep(0)


When I launch the worker at startup, everything works fine. If, however, I launch the worker from a view, I end up with a weird behaviour. Specifically, when I try to shutdown the server with a ctrl-c KeyboardInterrupt the server hangs – it stops handling new requests but doesn't terminate unless I do a OS kill.



Here's the full script:



from aiohttp import web
import logging
from logging.config import dictConfig
from aioprocessing import AioPipe, AioProcess
from asyncio import shield, sleep, get_event_loop

import uuid
import time


async def the_view(request):
msg = str(uuid.uuid4())[0:8]
if request.app.process is None:
logging.info("Launching worker process in view.")
await launch_worker(request.app)

pipe = request.app.pipe
logging.info("Webserver Sending {}".format(msg))
pipe.send(msg)
output = await shield(pipe.coro_recv())
logging.info("Webserver received {}".format(output))
return web.Response(text=output)


def worker(pipe):
try:
while True:
foo = pipe.recv()
if foo is None:
break
else:
time.sleep(3)
msg = str(uuid.uuid4())[0:8]
logging.info("Worker returning {} {}".format(foo, msg))
pipe.send("{} {}".format(foo, msg))
except KeyboardInterrupt:
pipe.send("Worker killed, argggghhhh")


async def launch_worker_at_start(app):
logging.info("Launching worker at start")
return await launch_worker(app)


async def launch_worker(app):
pipe_a, pipe_b = AioPipe()
process = AioProcess(target=worker, args=(pipe_b,))
process.start()
app.process, app.pipe = process, pipe_a
await sleep(0)


def build_app(loop):
app = web.Application(debug=True)
app.process, app.pipe = None, None
app.router.add_get('/', the_view)

## uncomment the following line and everything works ##
# loop.create_task(launch_worker_at_start(app))
#######################################################

return app


if __name__ == '__main__':
logging.info("Webserver starting")
loop = get_event_loop()
this_app = build_app(loop)

web.run_app(this_app, port=8081, loop=loop)


I'm using aiohttp==2.3.10 and aioprocessing==0.0.1



EDIT:



Same behaviour with aiohttp==3.4.4 and aioprocessing==1.0.1 and python 3.5.6.










share|improve this question





























    0















    I need to do some blocking cpu work, so I launch a new aioprocessing AioProcess and use an aioprocessing AioPipe to communicate with it.



    launching the process looks like this:



    async def launch_worker(app):
    pipe_a, pipe_b = AioPipe()
    process = AioProcess(target=worker, args=(pipe_b,))
    process.start()
    app.process, app.pipe = process, pipe_a
    await sleep(0)


    When I launch the worker at startup, everything works fine. If, however, I launch the worker from a view, I end up with a weird behaviour. Specifically, when I try to shutdown the server with a ctrl-c KeyboardInterrupt the server hangs – it stops handling new requests but doesn't terminate unless I do a OS kill.



    Here's the full script:



    from aiohttp import web
    import logging
    from logging.config import dictConfig
    from aioprocessing import AioPipe, AioProcess
    from asyncio import shield, sleep, get_event_loop

    import uuid
    import time


    async def the_view(request):
    msg = str(uuid.uuid4())[0:8]
    if request.app.process is None:
    logging.info("Launching worker process in view.")
    await launch_worker(request.app)

    pipe = request.app.pipe
    logging.info("Webserver Sending {}".format(msg))
    pipe.send(msg)
    output = await shield(pipe.coro_recv())
    logging.info("Webserver received {}".format(output))
    return web.Response(text=output)


    def worker(pipe):
    try:
    while True:
    foo = pipe.recv()
    if foo is None:
    break
    else:
    time.sleep(3)
    msg = str(uuid.uuid4())[0:8]
    logging.info("Worker returning {} {}".format(foo, msg))
    pipe.send("{} {}".format(foo, msg))
    except KeyboardInterrupt:
    pipe.send("Worker killed, argggghhhh")


    async def launch_worker_at_start(app):
    logging.info("Launching worker at start")
    return await launch_worker(app)


    async def launch_worker(app):
    pipe_a, pipe_b = AioPipe()
    process = AioProcess(target=worker, args=(pipe_b,))
    process.start()
    app.process, app.pipe = process, pipe_a
    await sleep(0)


    def build_app(loop):
    app = web.Application(debug=True)
    app.process, app.pipe = None, None
    app.router.add_get('/', the_view)

    ## uncomment the following line and everything works ##
    # loop.create_task(launch_worker_at_start(app))
    #######################################################

    return app


    if __name__ == '__main__':
    logging.info("Webserver starting")
    loop = get_event_loop()
    this_app = build_app(loop)

    web.run_app(this_app, port=8081, loop=loop)


    I'm using aiohttp==2.3.10 and aioprocessing==0.0.1



    EDIT:



    Same behaviour with aiohttp==3.4.4 and aioprocessing==1.0.1 and python 3.5.6.










    share|improve this question



























      0












      0








      0








      I need to do some blocking cpu work, so I launch a new aioprocessing AioProcess and use an aioprocessing AioPipe to communicate with it.



      launching the process looks like this:



      async def launch_worker(app):
      pipe_a, pipe_b = AioPipe()
      process = AioProcess(target=worker, args=(pipe_b,))
      process.start()
      app.process, app.pipe = process, pipe_a
      await sleep(0)


      When I launch the worker at startup, everything works fine. If, however, I launch the worker from a view, I end up with a weird behaviour. Specifically, when I try to shutdown the server with a ctrl-c KeyboardInterrupt the server hangs – it stops handling new requests but doesn't terminate unless I do a OS kill.



      Here's the full script:



      from aiohttp import web
      import logging
      from logging.config import dictConfig
      from aioprocessing import AioPipe, AioProcess
      from asyncio import shield, sleep, get_event_loop

      import uuid
      import time


      async def the_view(request):
      msg = str(uuid.uuid4())[0:8]
      if request.app.process is None:
      logging.info("Launching worker process in view.")
      await launch_worker(request.app)

      pipe = request.app.pipe
      logging.info("Webserver Sending {}".format(msg))
      pipe.send(msg)
      output = await shield(pipe.coro_recv())
      logging.info("Webserver received {}".format(output))
      return web.Response(text=output)


      def worker(pipe):
      try:
      while True:
      foo = pipe.recv()
      if foo is None:
      break
      else:
      time.sleep(3)
      msg = str(uuid.uuid4())[0:8]
      logging.info("Worker returning {} {}".format(foo, msg))
      pipe.send("{} {}".format(foo, msg))
      except KeyboardInterrupt:
      pipe.send("Worker killed, argggghhhh")


      async def launch_worker_at_start(app):
      logging.info("Launching worker at start")
      return await launch_worker(app)


      async def launch_worker(app):
      pipe_a, pipe_b = AioPipe()
      process = AioProcess(target=worker, args=(pipe_b,))
      process.start()
      app.process, app.pipe = process, pipe_a
      await sleep(0)


      def build_app(loop):
      app = web.Application(debug=True)
      app.process, app.pipe = None, None
      app.router.add_get('/', the_view)

      ## uncomment the following line and everything works ##
      # loop.create_task(launch_worker_at_start(app))
      #######################################################

      return app


      if __name__ == '__main__':
      logging.info("Webserver starting")
      loop = get_event_loop()
      this_app = build_app(loop)

      web.run_app(this_app, port=8081, loop=loop)


      I'm using aiohttp==2.3.10 and aioprocessing==0.0.1



      EDIT:



      Same behaviour with aiohttp==3.4.4 and aioprocessing==1.0.1 and python 3.5.6.










      share|improve this question
















      I need to do some blocking cpu work, so I launch a new aioprocessing AioProcess and use an aioprocessing AioPipe to communicate with it.



      launching the process looks like this:



      async def launch_worker(app):
      pipe_a, pipe_b = AioPipe()
      process = AioProcess(target=worker, args=(pipe_b,))
      process.start()
      app.process, app.pipe = process, pipe_a
      await sleep(0)


      When I launch the worker at startup, everything works fine. If, however, I launch the worker from a view, I end up with a weird behaviour. Specifically, when I try to shutdown the server with a ctrl-c KeyboardInterrupt the server hangs – it stops handling new requests but doesn't terminate unless I do a OS kill.



      Here's the full script:



      from aiohttp import web
      import logging
      from logging.config import dictConfig
      from aioprocessing import AioPipe, AioProcess
      from asyncio import shield, sleep, get_event_loop

      import uuid
      import time


      async def the_view(request):
      msg = str(uuid.uuid4())[0:8]
      if request.app.process is None:
      logging.info("Launching worker process in view.")
      await launch_worker(request.app)

      pipe = request.app.pipe
      logging.info("Webserver Sending {}".format(msg))
      pipe.send(msg)
      output = await shield(pipe.coro_recv())
      logging.info("Webserver received {}".format(output))
      return web.Response(text=output)


      def worker(pipe):
      try:
      while True:
      foo = pipe.recv()
      if foo is None:
      break
      else:
      time.sleep(3)
      msg = str(uuid.uuid4())[0:8]
      logging.info("Worker returning {} {}".format(foo, msg))
      pipe.send("{} {}".format(foo, msg))
      except KeyboardInterrupt:
      pipe.send("Worker killed, argggghhhh")


      async def launch_worker_at_start(app):
      logging.info("Launching worker at start")
      return await launch_worker(app)


      async def launch_worker(app):
      pipe_a, pipe_b = AioPipe()
      process = AioProcess(target=worker, args=(pipe_b,))
      process.start()
      app.process, app.pipe = process, pipe_a
      await sleep(0)


      def build_app(loop):
      app = web.Application(debug=True)
      app.process, app.pipe = None, None
      app.router.add_get('/', the_view)

      ## uncomment the following line and everything works ##
      # loop.create_task(launch_worker_at_start(app))
      #######################################################

      return app


      if __name__ == '__main__':
      logging.info("Webserver starting")
      loop = get_event_loop()
      this_app = build_app(loop)

      web.run_app(this_app, port=8081, loop=loop)


      I'm using aiohttp==2.3.10 and aioprocessing==0.0.1



      EDIT:



      Same behaviour with aiohttp==3.4.4 and aioprocessing==1.0.1 and python 3.5.6.







      python python-multiprocessing python-asyncio aiohttp






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 30 '18 at 13:49









      Sasha Tsukanov

      666418




      666418










      asked Nov 26 '18 at 10:47









      acrophobiaacrophobia

      424417




      424417
























          0






          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',
          autoActivateHeartbeat: false,
          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%2f53479463%2faiohttp-different-behaviour-when-aioprocessing-is-launched-from-view-vs-startu%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53479463%2faiohttp-different-behaviour-when-aioprocessing-is-launched-from-view-vs-startu%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

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)