Git Product home page Git Product logo

Comments (2)

OrangeTux avatar OrangeTux commented on August 22, 2024 2

Mixing ascynio with threads can be tricky. So I would avoid that if possible.

Instead of running ChargePoint.heartbeatFunc() in a new thread you should run it on the event loop of your main thread. For Python 3.7+ you can use asyncio.create_task. Fr older versions you can use asyncio.ensure_future.

 async def send_boot_notification(self):

        request = call.BootNotificationPayload(
            charge_point_model="sample",
            charge_point_vendor="test"
        )
        response = await self.call(request)
        #heartbeat on main thread ok
        request = call.HeartbeatPayload()
        response = await self.call(request)
        #send beartbeat on new thread time interval continuously ng
        asyncio.create_task(self.heartbeatFunc())

Getting back to the exception in your example: RuntimeError: Task attached to a different loop.
In order to understand that you must know that tasks and coroutines are executed on an event loop. There are several ways to start an event loop. But normally you start that event loop with asyncio.run in Python 3.7+ or asyncio.run_until_complete if you use an older version of Python.

This event loop is bound to a thread. All the tasks are also bound to that event loop. In the code example you moved moved a coroutine to a different thread to execute it there. That is not possible.
Therefore again my advice to avoid asyncio and threads.

from ocpp.

JavanWang avatar JavanWang commented on August 22, 2024

Amazing!
I have struggled with asyncio / threading for almost a week,
you solve my problem in one line code.

from ocpp.

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.