0

Here is the context: I made a Qt5 program to drive a sourcemeter, it applies an electrical signal to a sample and measures its reaction, plotting data to matplotlib in real time. It looks like this: enter image description here

Plotting is done by the main thread, measuring/applying is done in another thread so that display does not slow down measurements.

The program also has a button to take all the available data from the plot and store it in a .csv file, but I'm considering having it write the values one by one in real time instead. I would like to add an optional input for the user to add description, so the stored data would be the actual I/V/time columns sometimes interrupted by text like "t=60, the sample smells funny". Writing the description line to the file would be easy, it's just a push button connected to a tiny function. The problem is that my file is already open somewhere else. Is that a problem at all?, I actually don't know, and if yes:

How do I safely write this additional description text from the main thread, while the file is opened by the measurements thread?

Thanks in advance.

Guimoute
  • 4,407
  • 3
  • 12
  • 28
  • 2
    Is it a single file handle shared by both threads? If so, a lock should work. – ShadowRanger Nov 28 '18 at 13:10
  • Yes it's a single file that is shared. Ok thanks for the info. I am starting to read this https://stackoverflow.com/questions/489861/locking-a-file-in-python, but is there a lock library in particular that you would advise? – Guimoute Nov 28 '18 at 13:22

1 Answers1

1
  • Open the file in your main thread
  • Pass the file handle to your other thread
  • In each thread, always lock before writing to the file
mnistic
  • 10,866
  • 2
  • 19
  • 33