Python 打开大型Json文件块UI

Python 打开大型Json文件块UI,python,json,multithreading,pyside,Python,Json,Multithreading,Pyside,以下是loadDefinitions的作用: 因此,我创建了两个工人,将一些数据传递给他们的构造函数,并为每个工人创建一个线程,然后将他们移动到各自的线程中。我在线程启动并等待后立即启动加载程序。另一个线程将唤醒它,然后它运行loadDefinitions。问题似乎是loadDefinitions中的第一个json.load()。它打开并反序列化一个~8.75MB的json文件。另外两个只有约9KB。在此期间,用户界面冻结-我在这里遗漏了什么?为什么会发生这种情况 谢谢 编辑: 如果我将load

以下是loadDefinitions的作用:

因此,我创建了两个工人,将一些数据传递给他们的构造函数,并为每个工人创建一个线程,然后将他们移动到各自的线程中。我在线程启动并等待后立即启动加载程序。另一个线程将唤醒它,然后它运行loadDefinitions。问题似乎是loadDefinitions中的第一个json.load()。它打开并反序列化一个~8.75MB的json文件。另外两个只有约9KB。在此期间,用户界面冻结-我在这里遗漏了什么?为什么会发生这种情况

谢谢

编辑:
如果我将loadDefinitions替换为:

用户界面不会阻塞。问题似乎出在json.load上


因为文件太大,所以我选择在显示UI之前加载它。

看起来您的登录正在等待相同的锁?您是说互斥锁?将解锁互斥锁,然后等待条件。
class SharedData(QtCore.QObject):
   def __init__(self):
      super(SharedData, self).__init__()

      self.loader = None

      self.mutex = QtCore.QMutex()
      self.loader_created = QtCore.QWaitCondition()


class Bungie(QtCore.QObject):
   denied = QtCore.Signal()
   authenticated = QtCore.Signal()

   def __init__(self, data):
      super(Bungie, self).__init__()

      self.data = data
      self.bungie = bungie.Bungie()

   @QtCore.Slot(str, str)
   def login(self, username, password):
      ...
            try:
               self.bungie.login(username, password)
            except bungie.InvalidCredentials:
               ...
               self.denied.emit()
            else:
               self.authenticated.emit()

               with util.Locker(self.data.mutex):
                  self.data.loader = loader.Loader(self.bungie)
                  self.data.loader_created.wakeAll()
      ...


class Loader(QtCore.QObject):
   def __init__(self, data):
      super(Loader, self).__init__()

      self.data = data

   @QtCore.Slot()
   def start(self):
      with util.Locker(self.data.mutex):
         if self.data.loader is None:
            self.data.loader_created.wait(self.data.mutex)

      with util.Locker(self.data.mutex):
         if self.data.loader:
            self.data.loader.loadDefinitions()


class MainWidget(QtGui.QWidget):
   login = QtCore.Signal(str, str)

   def __init__(self, parent=None):
      super(MainWidget, self).__init__(parent)

      # UI 
      ...

      self.data = SharedData()

      self.bungie_thread = QtCore.Thread(parent=self)
      self.bungie = Bungie(self.data)
      self.bungie.moveToThread(self.bungie_thread)

      self.loader_thread = QtCore.Thread(parent=self)
      self.loader = Loader(self.data)
      self.loader.moveToThread(self.loader_thread)

      self.login.connect(self.bungie.login)

      self.loader_thread.started.connect(self.loader.start)

      self.bungie_thread.start()
      self.loader_thread.start()
   def loadDefinitions(self):
      if self.item_def is None:
         with open("manifest/DestinyInventoryItemDefinition_hashkey.json",
                   "r") as fin_json:
            self.item_def = json.load(fin_json)

      if self.bucket_def is None:
         with open("manifest/DestinyInventoryBucketDefinition_hashkey.json",
                   "r") as fin_json:
            self.bucket_def = json.load(fin_json)

      if self.stat_def is None:
         with open("manifest/DestinyStatDefinition_hashkey.json",
                   "r") as fin_json:
            self.stat_def = json.load(fin_json)
      numbers = []
      for _ in xrange(1000000):
         numbers.append(random.randint(1, 101))