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

    Freeze trying to open file on SD

    Scheduled Pinned Locked Moved MicroPython
    micropythonthreadthreadinglopysdcard
    6 Posts 2 Posters 2.1k Views 3 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.
    • A Offline
      Andrea Filippini
      last edited by

      Hello, I have a problem writing to the SD card from inside a thread. I really cannot reproduce it outside my application, so I hope someone can give some suggestions.

      I have a thread running alone which generates some data and put it in a buffer. Every once in a while a method is called to save the buffer in a file on the SD card. The thread and the method itself are running well but when open(filename) is executed the whole board freezes without any error or exception thrown.
      (At this point I'd like to ask another question: how to effectively print a thread's error output? When an exception is thrown inside a secondary thread the exception arguments are often useless)

      I tryed to put all the thread in an empty script and it works good (even running a bunch of them at the same time)

      Please take a look at the following function:

      def write_buffer_to_file(jbuffer, directory):
          if globals.sd != None:
              try:
      
                 /*  various check operations */
                 /*  and filename definition */
      
                      // thread.lock was the most obvious solution, but didn't help
                      a_lock = _thread.allocate_lock() 
                      with a_lock:
                          irq_st = machine.disable_irq() // extreme attempt, but no luck :/
                          print('acquired lock')
      
                          with open(filename, 'w') as f:
                              print('file opened')
                             /* write content */
      
                          machine.enable_irq(irq_st)
      
              except Exception as e:
                  print('write_buffer_to_file:', e.args)
                  globals.thread_error = True
      
      
      L 1 Reply Last reply Reply Quote 0
      • L Offline
        livius @Andrea Filippini
        last edited by

        @Andrea-Filippini said in Freeze trying to open file on SD:

        a_lock = _thread.allocate_lock()

        first i suppose you forgot to add

        a_lock.acquire()
        and 
        lock.release()
        

        and especially you must have this lock object outside of that thread
        because another thread will create new one lock object {if i understand correctly micropython implementation}

        think about people first, then computers

        A 1 Reply Last reply Reply Quote 0
        • A Offline
          Andrea Filippini @livius
          last edited by

          @livius using the with statement should manage lock's acquire and release. I think that the lock must be acquired inside the thread, it would be tricky not to do that.
          I admit the documentation is not very clear about the Lock class, but I tested this outside the application and the lock does his work

          L 1 Reply Last reply Reply Quote 0
          • L Offline
            livius @Andrea Filippini
            last edited by livius

            @Andrea-Filippini said in Freeze trying to open file on SD:

            using the with statement should manage lock's acquire and release

            ah i have omitted with statement..

            I think that the lock must be acquired inside the thread, it would be tricky not to do that.

            You misunderstand me here, allocate not accuire
            Accuire must be inside thread but allocate of lock object should not i suppose.

            something like that

            a_lock= _thread.allocate_lock() #allocate outside of thread
            
            def write_buffer_to_file(jbuffer, directory):
                with a_lock:
                    print('acquired lock')
            

            think about people first, then computers

            1 Reply Last reply Reply Quote 0
            • A Offline
              Andrea Filippini
              last edited by Andrea Filippini

              ACTUALLY THE FOLLOWING IS NOT TRUE ANYMORE (freeze is back):

              Problem has been solved using FTP file transfer instead of Atom Plugin....
              @Ralph maybe this is connected to these (https://github.com/pycom/pymakr-atom/issues/16 https://github.com/pycom/pymakr-atom/issues/13) issues?

              1 Reply Last reply Reply Quote 0
              • A Offline
                Andrea Filippini
                last edited by

                UP
                Anybody got a clue?

                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