0

I have problem with debugging my view function with import pdb; pdb.set_trace() placed inside it and serverless launched as > sls offline start in console. Namely, making correspondent GET request I receive the following error:

Python: > /.../handler.py(88)get_results()
-> request_params = event.query_params


Python: (Pdb)


Python: 2019-02-20 18:37:43,648 [ERROR] | ...
Traceback (most recent call last):
  ...
  File ".../handler.py", line 88, in get_results
  ...
  File "/usr/lib/python3.6/bdb.py", line 51, in trace_dispatch
    return self.dispatch_line(frame)
  File "/usr/lib/python3.6/bdb.py", line 70, in dispatch_line
    if self.quitting: raise BdbQuit
bdb.BdbQuit

Google suggests that the problem is in the inability of serverless process to read from stdin, but I don't know how to handle this problem.

Any suggestions?

Andriy
  • 1,270
  • 3
  • 17
  • 35
  • I usually import the handler in a REPL session and execute the functions that way. Have you tried something similar? – Milan Cermak Feb 21 '19 at 13:49
  • I’m sorry, I didn’t understand that. I run `sls offline star`. handler.py is invoked by sls. How do you import the handler? Could you give me a hint? – Andriy Feb 21 '19 at 15:13
  • `cd` to the directory where you have your `handler.py`, run `python` to start an interactive REPL session, do `import handler` and then execute `handler.get_results()` or whatever function you need to. HTH – Milan Cermak Feb 21 '19 at 15:38
  • It is not always a good idea as `get_result(event, context)` function depends on two variables and we should pass there some values. It would be handy to get that values from the actual http request rather than invent something. Besides, I have more complex situation where the function `get_result` is wrapped by decorator and actual arguments of my function are transformed into some classes instances. So, your approach doesn't fit my needs. – Andriy Feb 21 '19 at 20:07

1 Answers1

1

I found a solution here https://stackoverflow.com/a/26975795/4388451:

  1. create two fifos:

    mkfifo fifo_stdin mkfifo fifo_stdout

in one terminal

  1. In the same terminal open stdout on background, and write to stdin:

    cat fifo_stdout & cat > fifo_stdin

  2. In python code create the the pdb object, and use it:

    import pdb mypdb = pdb.Pdb(stdin=open('fifo_stdin', 'r'), stdout=open('fifo_stdout', 'w')) .... mypdb.set_trace()

  3. Run python code from the folder where fifos were placed (or place fifos in the first step in the folder with python code) in another terminal

Now I am able to use pdb in first console!

PS It is useful to use --noTimeout option while debugging: sls offline --noTimeout

Andriy
  • 1,270
  • 3
  • 17
  • 35