Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Question about asyncio #48

Open
dronord opened this issue Oct 5, 2022 · 1 comment
Open

Question about asyncio #48

dronord opened this issue Oct 5, 2022 · 1 comment

Comments

@dronord
Copy link

dronord commented Oct 5, 2022

Hello.
Are there any plans to use asyncio in this nice project?

@sobomax
Copy link
Member

sobomax commented Apr 23, 2024

Hi @dronord, frankly asyncio is just another method to provide non-blocking behavior to threading, which historically is our method of choice. Both have their advantages and disadvantages, but the functional difference is quite slim except threading would take advantage of multiple CPUs once GIL is lifted one day.

Such conversion is possible as proven by @space88man here: https://github.com/space88man/b2bua, however we are bit of conservative plus there is 10x more code in our private repo and related projects that share code such as rtp_cluster. Which makes it a bit challenging. At the same time, if you want to integrate sippy with async code that might be doable by just spinning either of them into its own thread and then communicate via callFromThread() or shared Queue between async and sippy.

Starting sippy stack in a thread is as trivial as:

from sippy.Core.EventDispatcher import ED2

class SIPfoo(threading.Thread):
    def run(self):
        ED2.my_ident = get_ident()
        rval = ED2.loop()

    def terminate(self):
        ED2.callFromThread(ED2.breakLoop)
        self.join()

This is a bit of a toy example, but you can find more functional one here on setting various parameters and initializing transaction manager:

https://github.com/sippy/Infernos/blob/696c6ecca82f03e90952572886b12af21a71444a/SIP/InfernSIP.py#L64

Then you can create a special queue, which you can create from your async code and invoke various functions (such as transaction manager) in that stack using the following the following pattern:

        rval = Queue()
        ED2.callFromThread(self.sip_stack.new_session, msg, rval, on_create=rval.put)
        sip_sess, rtp_sess = rval.get()
        return (sip_sess.id, self.rtp_actr, rtp_sess.sess_id)

Most of the functions of interest provide some kind of callback that you can then use with a partial and/or wrapper class with call() method to filter results (of catch error) and pass it on the queue.

Queue is a built-in object so it should provide all asyncio semantics. So that you can then go and do your async stuff and will be awaken when needed.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants