Now, I am writing a program that uses websocket to get messages and process them each time.
I would like to add a variable to the argument of on_message as shown below. Is it possible?
def on_message (ws, message, signal):
In the first place, as a basis of programming using websocket, I feel that it is better not to do complicated processing in def on_message, but I could not find any other method.
If anyone knows a good way, please let me know.
import sys import websocket import _thread def on_message (ws, message): # Now we are processing the message in this function. print ('--- RECV MSG. ---') print (message) def on_error (ws, error): print ('--- ERROR ---') print (error) def on_close (ws): print ('--- DISCONNECTED ---') def on_open (ws): print ('--- CONNECTED ---') def run (* args): while (True): line = sys.stdin.readline () if line! ='': print ('closing ...') ws.close () _thread.start_new_thread (run, ()) url ='ws: // *****/****/websocket' # websocket.enableTrace (True) ws = websocket.WebSocketApp (url, on_message = on_message, on_error = on_error, on_close = on_close) ws.on_open = on_open ws.run_forever ()
Answer # 1
As the questioner feels, it is annoying to write a program elegantly that keeps the websocket connected and processes the incoming message. Since on_message is specified in the library as a callback, you probably can't add an argument. Also, although it is not decided not to write complicated processing in on_message, it feels like a place to write complicated processing somehow.
In the previous question and answer, I tried to challenge such a request, and I think it will be helpful.
The communication process of websocket is well hidden in the class so that only the message can be processed on the main function side. Please forgive that the description of async/await is a little noisy. Based on this code, I think it will be easier for the questioner to write the code he wants to write.
Please note that there is a library with a misleading name called websockets, which is used.
- python - i want to add an article category sorting function on the django blog
- [swift] when declaring a variable, how to declare it is not a function/closure type despite the expression that assigns a value
- python - i want to get the same result as assigning a variable and writing it directly in a function
- python - meaning of dlib face detection function arguments
- processing using the len function when an integer value is obtained from python standard input
- python - i want to create a function that finds the standard deviation sigma from x, y
- please explain the function using the python dictionary
- i want to put it in another variable with python for
- python - i want to get rid of the error that the function is defined but not defined
- i want to dynamically change the value of a variable in python and execute it
- python - i'm looking for a method like the countif function in excel for pywin32
- python - i want to know the type of softmax function in pytorch
- python - arguments are passed even though the function inside the function is not in parentheses
- python - when using the cv2adaptivethreshold function in the binarization process of an image, an error occurs in medianblur and
- c language: a function with variable length arguments, the pointer address values are not linked well
- python 3x - i want to shorten the python function
- i want to create a function in python that all functions without class go through
- python - out of range of predict function used in image recognition (?)
- python - array elements cannot be used as function arguments ??
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- i want to call a child component method from a parent in vuejs
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- xcode - pod install [!] no `podfile 'found in the project directory