0

I am designing a python API which takes some parameters. My primary requirement for this API is that calling module should be notified in case of some event X and it should also wait for the execution to complete.

I have already thought of passing a callback function to get executed on event X but I cannot do that since that method is tightly coupled to the callee.

This is what I want to achieve:

test = API().run()

On Event X, control should come back to test and then I wait again on previous API.run to complete.

So something like this

    while (waiting on run to complete) or (X does not happens):
         doX()

Here I somehow want API to communicate to me X has happened

dejanualex
  • 3,872
  • 6
  • 22
  • 37
Deepanshu Arora
  • 375
  • 1
  • 5
  • 21
  • There are some synch mechanisms in `threading` that may be useful, without further info there's little more we can do. – Adirio Sep 08 '20 at 11:59
  • @Adirio can we get on a chat and I can answer your questions – Deepanshu Arora Sep 08 '20 at 12:01
  • Trust me, If you can't explain your problem simple enough to be in SO, then you don't understand your problem well enough. – Adirio Sep 08 '20 at 12:03
  • @Adirio I have added some more context , please let me know if still this is not clear – Deepanshu Arora Sep 08 '20 at 12:18
  • For example, you could use a `threading.Condition` (https://docs.python.org/3/library/threading.html#condition-objects). You wait on it and pass the condition to the API. When the API wants the other thread to wake up it calls the `notify` method. – Adirio Sep 08 '20 at 12:26
  • `threading.Event` (https://docs.python.org/3/library/threading.html#event-objects) should be another option. The caller would call to the event's `wait` method while the API runs, and the API will call the event's `set` method to wake up the other thread. – Adirio Sep 08 '20 at 12:30

0 Answers0