Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Terminating a thread after 5s

    Scheduled Pinned Locked Moved GPy
    threadtimeout
    3 Posts 2 Posters 350 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K Offline
      kjm
      last edited by

      I'm trying to figure out how to abandon a function call after 5s if the function hasn't returned a value by then.

      import _thread, time
      
      def _function(this, that):
        print('waiting for _function'); time.sleep(9); print("don't want to see this message")
      
      _thread.start_new_thread(_function, (1, 2))
      lock=_thread.allocate_lock()
      lock.acquire(1, 5)
      

      gives

      Running C:\Users\Kris\Google Drive\My Documents\Pycom\main\test.py
      
      >>>
      >>>
      waiting for _function
      >
      Pycom MicroPython 1.18.1.r7 [v1.8.6-849-d1c5ea9] on 2018-12-17; GPy with ESP32
      Type "help()" for more information.
      >>>
      >>> don't want to see this message
      
      

      isn't forcing the function to exit after 5s. Can someone show me how to achieve this?

      O 1 Reply Last reply Reply Quote 0
      • O Offline
        oligauc @kjm
        last edited by

        @kjm Please can you give more details on what you are trying to achieve ? What is the function doing ? To monitor the elapsed time you can do:

        def thread_proc():
            start_time = utime.time()
            while utime.time() - start_time < 5:
                time.sleep(0.01)
        
        _thead.start_new_thread(thread_proc, ())
        
        K 1 Reply Last reply Reply Quote 0
        • K Offline
          kjm @oligauc
          last edited by

          @oligauc Have realised thread locking was not what we were after. What we actually want to do is put getaddrinfo in a thread so that if it the DNS lookup takes forever or fails the main program can move on. Unfortunately, this is not what happens, the main program pauses & waits for the thread. https://forum.pycom.io/topic/5295/getaddrinfo in-a-thread

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom