itamarst / eliot

Eliot: the logging system that tells you *why* it happened
https://eliot.readthedocs.io
Apache License 2.0
1.1k stars 66 forks source link

Using Eliot with QThreads in Pyside2 #452

Closed zmitchell closed 2 years ago

zmitchell commented 4 years ago

Hi, I'm not sure if you know much about Qt, but I'm trying to use eliot to log from some "worker" threads that operate within a Qt application I've made. I see that there's the eliot.preserve_context API, but I'm not quite sure how to use it in this context (no pun intended).

So you have an idea of how this looks from the Qt side, you start by making a QObject subclass.

class MyWorker(QObject):
    def __init__(self):
        ...

Then you instantiate one of these workers and call moveToThread:

thread = QThread()
worker = MyWorker(...)
worker.moveToThread(thread)

My log entries show up when I log to a file, but they are out of order and show up with <unnamed> in them:

57c1ea47-1aa9-4fc3-a6e8-86c91504e132
+-- close/1 ⇒ started 2020-02-28 00:05:49Z ⧖ 0.000s
    +-- close/2 ⇒ succeeded 2020-02-28 00:05:49Z

3668164e-764e-4090-b7c0-e0f5411de67b
+-- <unnamed>
    |-- /2 2020-02-28 00:05:27Z
    |   +-- signal: time_axis
    +-- store_preamble/3 ⇒ succeeded 2020-02-28 00:05:27Z

Do you have any suggestions?

itamarst commented 4 years ago

preserve_context() is mainly there to ensure you don't reuse the same action twice. I suggest using the process-specific API (https://eliot.readthedocs.io/en/stable/generating/threads.html#cross-process-tasks):

  1. Serialize the task ID to a bytes object, add it to MyWorker when constructed.
  2. Inside MyWorker when it first starts doing work in a thread, deserialize it. You now have a top-level Action object. Store it on MyWorker.
  3. Use non-finishing contexts to make that the top-level action for callbacks/slot calls into MyWorke (https://eliot.readthedocs.io/en/stable/generating/actions.html#non-finishing-contexts)

Does that make sense?

zmitchell commented 4 years ago

This makes sense. Something isn't quite working right, but I can't tell what at the moment because Qt gobbles up exceptions raised in slots. Thanks for your help, I'll let you know if I have more questions!

itamarst commented 4 years ago

What are you working on, if I can ask?

zmitchell commented 4 years ago

I work in an ultrafast spectroscopy lab, and I’m writing a GUI application that controls the experiment (talks to hardware) and displays the data collected in real time.

On Feb 28, 2020, at 7:59 PM, Itamar Turner-Trauring notifications@github.com wrote:

 What are you working on, if I can ask?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

itamarst commented 4 years ago

Neat. I've enjoyed working with scientists in the past (did some image processing work for spatial gene sequencing), if there's any other way I can help let me know. And do ask again if you can't figure out the solution.