mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-08 07:27:29 -06:00
Reinitialise Zeroconf network socket upon refresh/start
Reinitialising Zeroconf entirely causes CPU usage to go through the proverbial roof. This is sort of a hack, since we're touching the _listen_socket variable inside Zeroconf, which it hasn't exposed. But it works to still be able to refresh Zeroconf after network switches, and to not have high CPU usage afterwards. Contributes to issue CURA-2497.
This commit is contained in:
parent
8a6d125478
commit
a31a4a1e90
1 changed files with 9 additions and 8 deletions
|
@ -1,7 +1,7 @@
|
||||||
from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin
|
from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin
|
||||||
from . import NetworkPrinterOutputDevice
|
from . import NetworkPrinterOutputDevice
|
||||||
|
|
||||||
from zeroconf import Zeroconf, ServiceBrowser, ServiceStateChange, ServiceInfo
|
from zeroconf import Zeroconf, ServiceBrowser, ServiceStateChange, ServiceInfo, new_socket
|
||||||
from UM.Logger import Logger
|
from UM.Logger import Logger
|
||||||
from UM.Signal import Signal, signalemitter
|
from UM.Signal import Signal, signalemitter
|
||||||
from UM.Application import Application
|
from UM.Application import Application
|
||||||
|
@ -20,7 +20,7 @@ import json
|
||||||
class NetworkPrinterOutputDevicePlugin(OutputDevicePlugin):
|
class NetworkPrinterOutputDevicePlugin(OutputDevicePlugin):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self._zero_conf = None
|
self._zero_conf = Zeroconf()
|
||||||
self._browser = None
|
self._browser = None
|
||||||
self._printers = {}
|
self._printers = {}
|
||||||
|
|
||||||
|
@ -53,16 +53,18 @@ class NetworkPrinterOutputDevicePlugin(OutputDevicePlugin):
|
||||||
self.startDiscovery()
|
self.startDiscovery()
|
||||||
|
|
||||||
def startDiscovery(self):
|
def startDiscovery(self):
|
||||||
self.stop()
|
|
||||||
if self._browser:
|
if self._browser:
|
||||||
self._browser.cancel()
|
self._browser.cancel()
|
||||||
self._browser = None
|
self._browser = None
|
||||||
self._old_printers = [printer_name for printer_name in self._printers]
|
self._old_printers = [printer_name for printer_name in self._printers]
|
||||||
self._printers = {}
|
self._printers = {}
|
||||||
self.printerListChanged.emit()
|
self.printerListChanged.emit()
|
||||||
# After network switching, one must make a new instance of Zeroconf
|
#After network switching, Zeroconf's network socket is no longer functional.
|
||||||
# On windows, the instance creation is very fast (unnoticable). Other platforms?
|
#Zeroconf must reinitialise its socket, but reinitialising Zeroconf causes massive CPU usage.
|
||||||
self._zero_conf = Zeroconf()
|
#So we only reinitialise Zeroconf's listening socket.
|
||||||
|
self._zero_conf.engine.del_reader(self._zero_conf._listen_socket)
|
||||||
|
self._zero_conf._listen_socket = new_socket() #Warning: Touching Zeroconf's privates! It has no functionality to reinitialise its own socket.
|
||||||
|
self._zero_conf.engine.add_reader(self._zero_conf.listener, self._zero_conf._listen_socket)
|
||||||
self._browser = ServiceBrowser(self._zero_conf, u'_ultimaker._tcp.local.', [self._onServiceChanged])
|
self._browser = ServiceBrowser(self._zero_conf, u'_ultimaker._tcp.local.', [self._onServiceChanged])
|
||||||
|
|
||||||
# Look for manual instances from preference
|
# Look for manual instances from preference
|
||||||
|
@ -123,7 +125,6 @@ class NetworkPrinterOutputDevicePlugin(OutputDevicePlugin):
|
||||||
|
|
||||||
## Stop looking for devices on network.
|
## Stop looking for devices on network.
|
||||||
def stop(self):
|
def stop(self):
|
||||||
if self._zero_conf is not None:
|
|
||||||
self._zero_conf.close()
|
self._zero_conf.close()
|
||||||
|
|
||||||
def getPrinters(self):
|
def getPrinters(self):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue