NordicSemiconductor / pynrfjprog

Python wrapper around the nrfjprog dynamic link library (DLL)
Other
74 stars 26 forks source link

Disk write cache file problem. #19

Closed w-xinjiang closed 2 years ago

w-xinjiang commented 3 years ago

The multi-threaded burning program is packaged into an application with pyinstaller.After the program is running, during the burning process, Jlink_x64.dll and cache files are continuously written to the folder C:\Users\Administrator\AppData\Local\Temp, and will not be recycled.Cause the storage of C drive to be filled. image image

Code:

import threading
from pynrfjprog import HighLevel
import time

class myThread (threading.Thread):
   def __init__(self, threadID, name, counter):
      threading.Thread.__init__(self)
      self.threadID = threadID
      self.name = name
      self.counter = counter
   def run(self):
      print ("Starting " + self.name)
      update_fw(self.name)
      program_app(self.name)
      print ("Exiting " + self.name)

def update_fw(snr):
    print("updating FW in {}".format(snr))
    probe = HighLevel.IPCDFUProbe(api, int(snr), HighLevel.CoProcessor.CP_MODEM)
    print("{} probe initialized". format(snr))
    probe.program("mfw_nrf9160_1.2.2.zip")
    print("{} programmed".format(snr))
    probe.verify("mfw_nrf9160_1.2.2.zip")
    print("{} Modem verified".format(snr))

def program_app(snr):
    app_probe = HighLevel.DebugProbe(api, int(snr) )

    program_options = HighLevel.ProgramOptions(
        erase_action=HighLevel.EraseAction.ERASE_ALL,
        reset = HighLevel.ResetAction.RESET_SYSTEM,
        verify = HighLevel.VerifyAction.VERIFY_READ
    )
    app_probe.program( "merged.hex" , program_options=program_options )

    app_probe.verify( "merged.hex" , HighLevel.VerifyAction.VERIFY_READ )
    print("{} Application verified".format(snr))

    app_probe.reset()

# Create new threads
while True:
    api = HighLevel.API()
    api.open()
    devices = api.get_connected_probes()
    threads = list()
    print(devices)
    for idx, device in enumerate(devices):
        threads.append(myThread(idx, device, idx))

    # Start new Threads
    for thread in threads:
        thread.start()

    print("waiting for all threads to complete")
    #wait for threads to finish
    for t in threads:
        t.join()

    api.close()
    time.sleep(5)    
print("Exiting Main Thread")
simtind commented 3 years ago

You need to call probe.close() to allow the library to clean up the dlls. You could also use context handlers as follows, that should guarantee that the cleanup is performed:

def update_fw(snr):
    print("updating FW in {}".format(snr))
    with  HighLevel.IPCDFUProbe(api, int(snr), HighLevel.CoProcessor.CP_MODEM) as probe:
        print("{} probe initialized". format(snr))
        probe.program("mfw_nrf9160_1.2.2.zip")
        print("{} programmed".format(snr))
        probe.verify("mfw_nrf9160_1.2.2.zip")
        print("{} Modem verified".format(snr))

def program_app(snr):
    with HighLevel.DebugProbe(api, int(snr) ) as app_probe :
        program_options = HighLevel.ProgramOptions(
            erase_action=HighLevel.EraseAction.ERASE_ALL,
            reset = HighLevel.ResetAction.RESET_SYSTEM,
            verify = HighLevel.VerifyAction.VERIFY_READ
        )
        app_probe.program( "merged.hex" , program_options=program_options )

        app_probe.verify( "merged.hex" , HighLevel.VerifyAction.VERIFY_READ )
        print("{} Application verified".format(snr))

        app_probe.reset()
simtind commented 2 years ago

From v10.13.0 and forward, nrfjprog no longer uses temporary files to implement the J-Link multiloading mechanism. Instead a method similar to MultiAPI is implemented in C++. This should resolve your issue, as we no longer generate any temporary files.