1

After reading a post here, I thought that was possible to send arguments to my protocol factory using a lambda function, but for some reason, it just doesn't work (it doesn't recognize any connection). Since create_server doesn't accept arguments, how could I tell my protocol some useful information? I start a bunch of them using a loop for every door in a list, but after that, I can't relate to which protocol is which.

Any ideas?

1 Answers1

-1

Alright, I found the problem.

Instead of using the lambda like in the example:

await asyncio.start_server(lambda r, w: handle_client(r, w, session),
                           '', 55555)

I should be using lambda like this:

await asyncio.start_server(lambda: handle_client(r, w, session),
                           '', 55555)

I hope this may be helpful to someone else.

  • Did you actually test this by connecting to the server? I don't see how it can possibly work, given that the callback passed to `start_server` is called with two arguments, the reader and the writer stream. Your lambda accepts no arguments and will raise an exception when invoked as the `start_server` callback. Also, the variables `r` and `w` you pass to `handle_client` are not defined anywhere, so even if the lambda could somehow be invoked, it would immediately raise a `NameError`. – user4815162342 Apr 11 '21 at 07:30
  • Hey @user4815162342 , look. I was not running that exact piece of code, but instead, I had it adapted to my own needs (i did not post it because it is a mess and would shift attention away from the problem). When it did not work, I mess around until I come to the conclusion that the problem was the lambda. A day after asking the question, I figure out the solution (mostly by trial and errors because I never used a lambda before), and since the question was not being answered, I answer it hoping that someone with the exactly same problem could fix it. Maybe I should have deleted it, IDK... – Guilherme Richter Apr 12 '21 at 13:36
  • It's up to you what to do with the answer - my point was just that the code and advice in this answer is outright incorrect and as such unlikely to help future visitors. – user4815162342 Apr 12 '21 at 14:10