Python线程非常慢

时间:2015-09-09 09:10:08

标签: python gtk multiprocessing pygtk

我是python编程的新手。 我开始了一个得分表项目。这个概念非常简单。 2个玩家,应该提高分数并增加计时器。到目前为止没问题。 此外,整个事情应该由Wii遥控器控制。 我开始使用线程实现wii-connection代码。如果我现在执行代码并连接wii控制器,执行变得非常慢,输出只是零星的。 奇怪的是,如果我现在将strace附加到该过程它运行正常,如果它已经杀死它再慢,等等。 有没有人看到过这种行为? 之后我尝试使用多处理实现wii连接。 这到目前为止工作正常,但现在我遇到的问题是我无法调用其他窗口(gtk),因为2个进程访问相同的处理程序以连接到X服务器。 我该怎么做才能让它运转起来? 提前谢谢了, 最好的问候,

尼科

应该启动

main.py来调用WiiRemote.py。成功连接后,应该从main.py调用WiiWindow.py来关闭窗口并打开下一个窗口。

main.py:

#!/usr/bin/env python
# coding=UTF-8

try:
  pygtk.require('2.0')
except:
  pass

import gobject
from Tafel import Tafel
from Wahl  import Wahl
from WiiWindow import WiiWindow
from WiiRemote import WiiRemote
#from thread import start_new_thread
#import threading

class Main:
  def __init__(self):
    self.Tafel = Tafel()
    self.WiiWindow = WiiWindow()
    self.Wahl = Wahl("","")
    self.Wii = WiiRemote(self.callbackWii, self.wiiConnected)
    gobject.timeout_add(1, self.Wii.keepRunning)
    self.currentScreen = 0
    self.showWii()

  def showTafel(self):
    self.currentScreen = 2
    self.Tafel.show()
    self.Tafel.run()

  def showWahl(self):
    self.currentScreen = 1
    gobject.timeout_add(500, self.Wii.keepRunning)
    self.Wahl.show()
    #self.Wahl.run()

  def showWii(self):
    self.currentScreen = 0
    self.WiiWindow.show()
    self.WiiWindow.run()

  def wiiConnected(self, isConnected):
    print("isConn: ",isConnected)
    if (isConnected == True):
      self.WiiWindow.Connected()
      self.showWahl()
    else:
      self.wii.destroy()


  def callbackWii(self, btn):
    print ("wiiClicked: ", btn)
    return
    if (self.currentScreen == 1):
      self.Wahl.callbackWii(btn)
    if (self.currentScreen == 2):
      self.Tafel.callbackWii(btn)




main = Main()

WiiRemote.py:

import cwiid
import time
import threading

class WiiRemote(threading.Thread):

  def __init__(self, callbackClicked, callbackConnected):
    threading.Thread.__init__(self)
    self.thread = self
    self.thread.setDaemon = True
    self.thread.start()
    self.callback = callbackClicked
    self.connected = callbackConnected
    self.running = True

  def keepRunning(self):
    return True

  def destroy(self):
    self.running = False

  def run(self):

    while True:
      print 'break'
      time.sleep(1)


    self.button_delay = 0.2
    print 'Press 1 + 2 on your Wii Remote now ...'
    time.sleep(1)

    # Connect to the Wii Remote. If it times out
    # then quit.
    try:
      wii=cwiid.Wiimote()
    except RuntimeError:
      print "Error opening wiimote connection"
      self.connected(False)
      quit()
    self.connected(True)
    print 'Wii Remote connected...\n'
    print 'Press some buttons!\n'
    print 'Press PLUS and MINUS together to disconnect and quit.\n'

    wii.rpt_mode = cwiid.RPT_BTN
    while self.running == True:

      buttons = wii.state['buttons']

      # If Plus and Minus buttons pressed
      # together then rumble and quit.
      if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0):
        print '\nClosing connection ...'
        wii.rumble = 1
        time.sleep(1)
        wii.rumble = 0
        exit(wii)

      # Check if other buttons are pressed by
      # doing a bitwise AND of the buttons number
      # and the predefined constant for that button.
      if (buttons - cwiid.BTN_UP == 0):
        self.callback("UP")
        time.sleep(self.button_delay)

WiiWindow.py

try:
  import pygtk
  import gobject
  pygtk.require('2.0')
  import gtk
  import time
except:
  pass

RCCONN = False

class WiiWindow(object):
  def __init__(self):
    self.builder = gtk.Builder()
    self.builder.add_from_file("wahl.glade")
    self.wwindow = self.builder.get_object("window1")
    self.BUTworc = self.builder.get_object('BUTworc')  

  def run(self):
    print 'start'
    gtk.main()


  def show(self):
    self.wwindow.show()

  def Connected(self):
    self.wwindow.destroy()

0 个答案:

没有答案