drako234
drako234

Reputation: 129

Python asyncio resource unavailable

I have a python server that is supposed to continuously send data and wait for prompts from the client to stop, set a variable or transfer the contents of a log file. currently i'm just trying to revive a prompt from the client.

I am attempting to utilize asyncio to do this but I am running into the following error:

(, BlockingIOError(11, 'Resource temporarily unavailable'), )

my server code is:

loop = asyncio.get_event_loop()

async def server():
        s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

        host = ''
        port = 1194

        s.bind((host,port))
        print ("bouned to port")
        s.listen(4)
        while True:

                conn, addr = await loop.sock_accept(s)

                if conn:
                        print ("connected to address: {!r}".format(addr))
#                        print (conn)
                        d = loop.create_task(temp_sender(conn))
                        try:
                            await d
                        except StopIteration:
                            pass
async def temp_sender(conn):
        print("temp_sender activated")

        try:
                data = await conn.recv(1024)
                print (data)
                sdata = data.decode()
                print (sdata)
                conn.writeline("the set temp is: ", sdata)
        except:
                print (sys.exc_info())
                pass

the error occurs in the temp_sender function when trying to receive data from conn.

how can I fix this availability error?

Upvotes: 1

Views: 537

Answers (1)

Aravind
Aravind

Reputation: 103

import asyncio
@asyncio.coroutine
def handle_echo(reader, writer):
    data = yield from reader.read(1024)
    # reader reads from client
    #writer writes to client

    message = data
    addr = writer.get_extra_info('peername')
    print("Received %r from %r" % (message, addr))

    print("Send: %r" % message)
    writer.write(data)
    yield from writer.drain()

    print("Closing client socket")
    writer.close()

loop = asyncio.get_event_loop()
##the code below will run whenever a new client connects ( multiple at a time) 
coro = asyncio.start_server(handle_echo, '127.0.0.1',1194, loop=loop)
server = loop.run_until_complete(coro)

# Serve requests until Ctrl+C is pressed
print('Serving on {}'.format(server.sockets[0].getsockname()))
try:
    loop.run_forever()
except KeyboardInterrupt:  #server stops on a keyboard interrupt
    pass

# Closing the server
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()

Upvotes: 1

Related Questions