[Python] Is there a way to take advantage of multiple CPU cores with asyncio?

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    I've created a simple HTTP Server with python and asyncio. But, I have read that asyncio-based servers can only take advantage of one CPU core. I am trying to fix this with multiprocessing and it doesn't work. When I try to create a process, It gives me an error saying that it cannot create the process from _winapi. This is my code so far:

    def serve_forever(self, host, port):
    srv, loop = self.init(host, port)
    print()
    if self.name:
    print('* Serving App {}'.format(self.name))
    print('* Serving On http://{host}:{port}'.format(host=host, port=port))
    print('* Press <CTRL-C> To Quit')
    workers = []
    try:
    for i in range(mp.cpu_count()-1):
    p = mp.Process(target=loop.run_forever)
    workers.append(p)
    p.start()
    loop.run_forever()
    except KeyboardInterrupt:
    print()
    for p in workers:
    p.terminate()
    srv.close()
    loop.run_until_complete(srv.wait_closed())
    loop.close()
    pass


    By the way, the self.init function works.

    Continue reading...

Compartilhe esta Página