GithubHelp home page GithubHelp logo

Question about asyncio about b2bua HOT 1 OPEN

sippy avatar sippy commented on August 28, 2024
Question about asyncio

from b2bua.

Comments (1)

sobomax avatar sobomax commented on August 28, 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.

from b2bua.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.