Giter VIP home page Giter VIP logo

Comments (2)

Tinanuaa avatar Tinanuaa commented on June 12, 2024 1

Thanks for the detailed reply, thats' really helpful, and I tried to use async for to handle the received data, it works!

from caproto.

klauer avatar klauer commented on June 12, 2024

Thanks for reporting this. There's an underlying bug causing that message, but also an issue with the code you have provided.

Suggestions for your code

In your code, add_callback(f) happens and start returns almost immediately. That means that the closure f gets garbage collected (along with the context and everything else).

You need to keep that function alive for a time period, such as adding an await asyncio.sleep(5) to sleep for 5 seconds after you add the callback.

Alternatively, you might consider using the async with and async for syntax which is a bit more ergonomic than using callbacks. See an example here:

async with chan1.subscribe() as sub:
sub.add_callback(user_async_callback)
sub.add_callback(user_callback)
async for value in sub:

async def start():
    ctx = Context()
    (array_data,) = await ctx.get_pvs("{pv_name}")
    sub = array_data.subscribe()
    responses = []
    def f(sub, response):
        print("Received response from", sub.pv.name)
        responses.append(response)
    token = sub.add_callback(f)
    await asyncio.sleep(10)

or

async def start():
    ctx = Context()
    (array_data,) = await ctx.get_pvs("{pv_name}")
    responses = []
    async with array_data.subscribe() as sub:
        async for value in sub:
            print("Received response from", sub.pv.name, value)
            responses.append(value)

The bug you helped uncover (re-uncover?)

The conversion of the threaded client to asyncio was never fully vetted. The background of the caproto async client issue that needs resolving is:

  1. CallbackHandler has a synchronous remove_callback method:
    def remove_callback(self, token):
  2. When your callback function gets garbage collected, remove_callback is automatically called as in here:
    self.remove_callback(cb_id) # defined below inside the lock
  3. However, the subclass Subscription reimplements (!) remove_callback as async (the horror...):
    async def remove_callback(self, token):
  4. This means that garbage-collected Subscription callbacks will call the subclassed async method but never await them

I'd have to dive deeper to figure out if reimplementing CallbackHandler to be async or decoupling Subscription from the superclass would be better.

from caproto.

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.