I am writing something in the spirit of a calculator. The input is a string with an expression. The problem is that it can be very difficult to calculate (the answer will be sooo long, for me it is critical). If such an expression arrives at the input, it is necessary to inform that it cannot be processed. I thought about the number of characters, but the expression 12 ** 123456 is not very long, but it will be processed for a long time. MB somehow do it on a timer?

  • one
    You can independently input for your case, try parsing . In general, sympy can call a long C function without releasing GIL, so the only reliable way to implement a timeout is to use an external process ( multiprocessing , subprocess ). Describe in more detail what input format, on which systems the program will run. - jfs
  • Perhaps input expressions to sort on complexity ? - Mr. Brightside

0