Connecting twice with twisted - how to do that correctly?

后端 未结 4 1996
一生所求
一生所求 2021-02-09 15:42

I want to use twisted (and StarPy which is a protocol implementation for asterisk ami) to connect to an asterisk server. The application initiates a outgoing fax there. I found

4条回答
  •  深忆病人
    2021-02-09 16:21

    Thanks for your answers, I haven't implemented a solution right now but I know how I could do that now...

    Here a short summary of things I learned.

    First, in short - the problems I had with twisted:

    1. I didn't understand the asynchronous basics of twisted. I used something like that in gui-frameworks but didn't see the benefit for a long time.
    2. Second, I tried to think of an synchronous call of the event loop multiple times. This was necessary in my mind because I may not use more than one outgoing fax line at a time. Because the event loop of twisted is not restartable this is no option. As I could read in the docs "deferToThread" could help me here, but I think it's not the best solution.

    In my concept I solved these problems:

    • Use of defer.DeferredSemaphore() which allows me to limit concurrency to 1. I found an example here: http://oubiwann.blogspot.com/2008/06/async-batching-with-twisted-walkthrough.html
    • calling twisted anynchronously and let the Defereds do the work (send a fax)
    • Writing the fax status to the database and sending the receipt mail is called from the defereds

    I needed a lot of re-thinking but as soon as you get it it looks really easy.

    Thanks to iny and Jean-Paul Calderone for your help.

提交回复
热议问题