Skip to content

How to pass user data to protocol's callbacks? #98

@zapta

Description

@zapta

Below is the OutputProtocol example from the documentation. Is there a way to pass to it user data that is configured at instantiation time? E.g. passing to serial_asyncio.create_serial_connection() a user value that will be propagated to the callback functions of OutputProtocol's ?

The difficulty I have is 1) I don't control the instantiation of this object and 2) I am not familiar with *args and **kwargs in case they hold the answer.

I could try to set the value myself in the protocol returned by serial_asyncio.create_serial_connection() but is there a guarantee that it will be called before the first callback is invoked?

import asyncio
import serial_asyncio

class OutputProtocol(asyncio.Protocol):
    def connection_made(self, transport):
        self.transport = transport
        print('port opened', transport)
 
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions