Freeze trying to open file on SD



  • 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
    
    


  • UP
    Anybody got a clue?



  • 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?



  • @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')
    


  • @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



  • @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}



Looks like your connection to Pycom Forum was lost, please wait while we try to reconnect.