如何在 C++ 和 django 通道之间进行通信?

How to communicate between C++ and django channels?

我正在使用 Django 频道做 Django 项目。在我的项目中,我需要在 C++ 代码之间进行集成和通信。在这两者之间,我必须向 C++ 发送一些值。 我在没有 django 频道的情况下进行了检查,它工作正常,任何人都可以帮助我如何在 django 频道的 consumers.py 中实现以下代码。

proc = subprocess.Popen(["/home/dev/integrated_module/front-face-camera/rest_project/CameraApplication/MakoContinuousFrameGrabberConsoleApplication" + ' ' + team_name + ' ' + user_name + ' ' + media_path],shell=True,preexec_fn=os.setsid,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

我已经使用具有同步功能的 Syncconsumer 找到了答案

class CameraParallelAsync(SyncConsumer):
    def websocket_connect(self,event):
        print("connected",self.channel_name)
        print('Data after delay')
        self.channel_group_name = "task_single_snap"
        self.send({
            "type":"websocket.accept"
        })

    def websocket_receive(self,event):
        print("receive",event)
        channel_data = event.get('text',None)
        user_name = self.scope['url_route']['kwargs']['user']
        team_name = self.scope['url_route']['kwargs']['team']
        media_path = settings.MEDIA_ROOT
        channel_name  = self.channel_name
        if channel_data is not None:
            if channel_data =='CAMERA_ON':
                self.scope['session']['camera'] = True
                print('Camera Enabled')
                global proc
                os.environ['LD_LIBRARY_PATH'] = "/usr/local/lib/Libraries"    
                import subprocess
                proc = subprocess.Popen(["/home/dev/integrated_module/front-face-camera/rest_project/CameraApplication/MakoContinuousFrameGrabberConsoleApplication" + ' ' + team_name + ' ' + user_name + ' ' + media_path],shell=True,preexec_fn=os.setsid,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
       
                self.send({
                    "type":"websocket.send",
                    "text":"Camera Enabled"
                })
            elif channel_data == 'CAMERA_OFF':
               print('Camera disabled')
               self.scope['session']['camera'] = False
               import signal
               os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
            elif channel_data == 'SINGLE_SNAP':
               self.send({
                   "type":"websocket.send",
                   "text":"We are processing your request"
               })
               print('Single Snap')
               task = tasks.camera_single_task.delay(user_name,team_name,media_path,channel_name)
               print(task.status,'status')

            elif channel_data == 'GROUP_SNAP':
                print('Group Snap')
                os.environ['LD_LIBRARY_PATH'] = "/usr/local/lib/Libraries"
                import subprocess
                proc = subprocess.Popen(["/home/dev/integrated_module/front-face-camera/rest_project/CameraApplication/MakoGroupFrameGrabberConsoleApplication" + ' ' + team_name + ' ' + user_name + ' ' + media_path],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
                print(proc.communicate())
                self.send({
                       "type":"websocket.send",
                       "text":"Group snap Snaptured"
                   })
            else:
               print('No detection')



    def websocket_disconnect(self,event):
        print("disconnected",event)

        # Receive message from room group  
    def task_message(self, event):
        message = event['message']
        print('hello')
        # Send message to WebSocket
        self.send(text_data=json.dumps({
            'message': message
        }))