Python. How do you complete exec(s) at the given time?



  • Hi! I'd like to do a code on the python to test the program. Let's say I have the code I'm launching through. exec()♪ How can I do that after 1 second, the program downloads it and withdraws it Time limit exceeded?



  • Somehow, it is possible to:

    • to establish a separate process to be carried out exec
    • Wait a second and if it's a process exec It's not over yet.
    from multiprocessing import Process
    from time import sleep
    

    def run_exec():
    print('запуск процесса')
    exec('sleep(3)')
    print('процесс завершен')

    if name == 'main':
    t = Process(target=run_exec)
    t.start()
    sleep(2)
    if t.is_alive():
    t.terminate()
    print('процесс прерван')

    You can play with the values. sleep Main code and process to make sure that everything works, either the process succeeds in working for a certain time, or it is forced to stop.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2