Erick Amoedo
Erick Amoedo

Reputation: 485

Python: Print only one time inside a While

I've been using the while to catch a print and now i'm looking a way to just 1 print without break my while:

import wmi
import time


device_connected_wql = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA \'Win32_Keyboard\'"
device_disconnected_wql = "SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA \'Win32_Keyboard\'"

c = wmi.WMI()
connected_watcher = c.watch_for(raw_wql=device_connected_wql)
disconnected_watcher = c.watch_for(raw_wql=device_disconnected_wql)

while 1:
    try:
        connected = connected_watcher(timeout_ms=1)
    except wmi.x_wmi_timed_out:
      pass
    else:
        if connected:
            
              print("Keyboard connected")
      


    try:
        disconnected = disconnected_watcher(timeout_ms=1)
    except wmi.x_wmi_timed_out:
      pass
    else:
        if disconnected:
          
              print("Disconnected")
          

but what I get in the Output is this:

Keyboard connected
Keyboard connected
Keyboard connected
Keyboard connected
Keyboard connected
Keyboard connected
Keyboard connected
Keyboard connected
Keyboard connected
Keyboard connected
Keyboard connected
Keyboard connected

i will apreciate some suggestion

Upvotes: 0

Views: 103

Answers (3)

Prune
Prune

Reputation: 77837

I infer that what you're trying to do is to connect and disconnect as rapidly as you can, and print the message when the status changes. In that case, what you're looking for is that change. You need to implement the appropriate state machine. At the moment, your process fails as soon as you loop back to the top, because you never reset your connection Flags. Instead, try a simple stat variable:

connected = False
while True:
    # loop until connected
    while not connected:
        try:
            connected = connected_watcher(timeout_ms=1)
        except wmi.x_wmi_timed_out:
            pass

    print("Keyboard connected")

    # loop until disconnected
    while connected:
        try:
            connected = not disconnected_watcher(timeout_ms=1)
        except wmi.x_wmi_timed_out:
            pass

    print("Keyboard disconnect")

Is that what you need?

Upvotes: 0

dgw
dgw

Reputation: 13646

Save the state in a variable and print only if the state changes:

state = 'unknown'
while 1:
    try:
        connected = connected_watcher(timeout_ms=1)
    except wmi.x_wmi_timed_out:
      pass
    else:
        if connected and state != 'connected':
            state = 'connected'
            print("Keyboard connected")

    try:
        disconnected = disconnected_watcher(timeout_ms=1)
    except wmi.x_wmi_timed_out:
      pass
    else:
        if disconnected and state != 'disconnected':
            state = 'disconnected'
            print("Disconnected")
          

Upvotes: 3

DataGuy
DataGuy

Reputation: 198

Once it is connected it will always be connected. You need to end the while loop once it gets connected.

I would do this:

if connected:
            
     print("Keyboard connected")
     break

You could also create a variable and replace the value "1". Then if you don't want to use break you could change the variable value.

Upvotes: 0

Related Questions