-1

my subscribe function will call on_message function everytime that have message from websocket and unsubscribe when message is STOP

is_sub = True
def on_message(msg):
    print(msg)
    if msg == "STOP":
        unsubscribe(key)
        is_sub = False
        #continue to code

print("Start")
key = subscribe(on_message)  
while is_sub:
    print("on sub")
    time.sleep(1)
#cotinue here
print("End")

without while loop the code will end and not recive any more message. I want to find the better way (without time.sleep) to interrupt and continue the code

ps. I can't edit subscribe and unsubscribe function

  • 1
    I'm not sure I understand what you're asking for. Are you wanting the `print("End")` code to run before you unsubscribe, or are you having an issue with loop never breaking at all? I think you need `global is_sub` in the `on_message` function for it to work. – Blckknght Jun 25 '20 at 05:58
  • Your code is missing the subscribe function - currently this is just a NameError happening. – Patrick Artner Jun 25 '20 at 06:02
  • The `is_sub` inside your function is a local variable that has no connection to the global scoped `is_sub` - read [short-description-of-the-scoping-rules](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules) – Patrick Artner Jun 25 '20 at 06:04

3 Answers3

0

You could run the instance of your websocket server in a separate thread. This will allow you to still continue to run your main thread while your webserver is listening to incoming messages.

po.pe
  • 1,047
  • 1
  • 12
  • 27
  • I think this may be the opposite of what the questioner needs. They seem to *want* the main thread to block while the websocket does its thing, but there doesn't seem to be a great way to wait for it to finish. Their current code is a loop with a `sleep` call, and they want a better way. – Blckknght Jun 25 '20 at 06:02
  • Then I completely missed the point, my assumption was that the problem is that the server needs to be blocking to be able to constantly receive messages. – po.pe Jun 25 '20 at 06:06
  • Yeah, I'm not certain of my interpretation, the question is not very clear. – Blckknght Jun 25 '20 at 06:11
0

I think you can't do it without a while loop. If you want to free your main thread you can do your message subscription on separate thread but that's not what you want I guess. Most WebSocket client would provide a method to keep the connection alive. for example this provides a run_forever() methods which does the 'while loop' part for you. If you are using a client there is a chance that they provide methods for keeping the connection alive. I suggest you go through the documentation once more.

mursalin
  • 1,151
  • 1
  • 7
  • 18
-1

If you want to continue the code, you could use the continue() command and if you want to stop, you could use the break() command.

The Laggy Tablet
  • 100
  • 1
  • 12
  • 1
    It's not very clear what you're talking about here. `continue` and `break` are keywords in Python. They're independent statements, not functions you need to call. – Blckknght Jun 25 '20 at 06:03