Exemple #1
0
class BTApp(wx.App):
    """Base class for all wx-based BitTorrent applications"""

    def __init__(self, *a, **k):
        self.doneflag = threading.Event()
        wx.App.__init__(self, *a, **k)

    def OnInit(self):
        self.running = True
        if profile:
            try:
                os.unlink(prof_file_name)
            except:
                pass
            self.prof = Profiler()
            self.prof.enable()
        
        wx.the_app = self
        self._DoIterationId = wx.NewEventType()
        self.Connect(-1, -1, self._DoIterationId, self._doIteration)
        self.evt = wx.PyEvent()
        self.evt.SetEventType(self._DoIterationId)
        self.event_queue = []

        # this breaks TreeListCtrl, and I'm too lazy to figure out why
        #wx.IdleEvent_SetMode(wx.IDLE_PROCESS_SPECIFIED)
        # this fixes 24bit-color toolbar buttons
        wx.SystemOptions_SetOptionInt("msw.remap", 0)
        icon_path = os.path.join(image_root, 'bittorrent.ico')
        self.icon = wx.Icon(icon_path, wx.BITMAP_TYPE_ICO)
        return True

    def OnExit(self):
        self.running = False
        if profile:
            self.prof.disable()
            st = Stats(self.prof.getstats())
            st.sort()
            f = open(prof_file_name, 'wb')
            st.dump(file=f)

    def who(self, _f, a):
        if _f.__name__ == "_recall":
            if not hasattr(a[0], 'gen'):
                return str(a[0])
            return a[0].gen.gi_frame.f_code.co_name
        return _f.__name__

    def _doIteration(self, event):

        if self.doneflag.isSet():
            # the app is dying
            return

        _f, a, kw = self.event_queue.pop(0)

##        t = bttime()
##        print self.who(_f, a)
        _f(*a, **kw)
##        print self.who(_f, a), 'done in', bttime() - t
##        if bttime() - t > 1.0:
##            print 'TOO SLOW!'
##            assert False

    def CallAfter(self, callable, *args, **kw):
        """
        Call the specified function after the current and pending event
        handlers have been completed.  This is also good for making GUI
        method calls from non-GUI threads.  Any extra positional or
        keyword args are passed on to the callable when it is called.
        """

        # append (right) and pop (left) are atomic
        self.event_queue.append((callable, args, kw))
        wx.PostEvent(self, self.evt)

    def FutureCall(self, _delay_time, callable, *a, **kw):
        return wx.FutureCall(_delay_time, self.CallAfter, callable, *a, **kw)
Exemple #2
0
    t = task.LoopingCall(push)
    t.start(freq)
    
##    m = MultiRateLimiter(sched=rawserver.add_task)
##    m.set_parameters(120000000)
##    class C(object):
##        def send_partial(self, size):
##            global_rate.print_rate(size)
##            rawserver.add_task(0, m.queue, self)
##            return size
##            
##    m.queue(C())

    if profile:
        try:
            os.unlink(prof_file_name)
        except:
            pass
        prof = Profiler()
        prof.enable()

    rawserver.listen_forever()

    if profile:
        prof.disable()
        st = Stats(prof.getstats())
        st.sort()
        f = open(prof_file_name, 'wb')
        st.dump(file=f)

Exemple #3
0
class BTApp(wx.App):
    """Base class for all wx-based BitTorrent applications"""
    def __init__(self, *a, **k):
        self.doneflag = threading.Event()
        wx.App.__init__(self, *a, **k)

    def OnInit(self):
        self.running = True
        if profile:
            try:
                os.unlink(prof_file_name)
            except:
                pass
            self.prof = Profiler()
            self.prof.enable()

        wx.the_app = self
        self._DoIterationId = wx.NewEventType()
        self.Connect(-1, -1, self._DoIterationId, self._doIteration)
        self.evt = wx.PyEvent()
        self.evt.SetEventType(self._DoIterationId)
        self.event_queue = []

        # this breaks TreeListCtrl, and I'm too lazy to figure out why
        #wx.IdleEvent_SetMode(wx.IDLE_PROCESS_SPECIFIED)
        # this fixes 24bit-color toolbar buttons
        wx.SystemOptions_SetOptionInt("msw.remap", 0)
        icon_path = os.path.join(image_root, 'bittorrent.ico')
        self.icon = wx.Icon(icon_path, wx.BITMAP_TYPE_ICO)
        return True

    def OnExit(self):
        self.running = False
        if profile:
            self.prof.disable()
            st = Stats(self.prof.getstats())
            st.sort()
            f = open(prof_file_name, 'wb')
            st.dump(file=f)

    def who(self, _f, a):
        if _f.__name__ == "_recall":
            if not hasattr(a[0], 'gen'):
                return str(a[0])
            return a[0].gen.gi_frame.f_code.co_name
        return _f.__name__

    def _doIteration(self, event):

        if self.doneflag.isSet():
            # the app is dying
            return

        _f, a, kw = self.event_queue.pop(0)

        ##        t = bttime()
        ##        print self.who(_f, a)
        _f(*a, **kw)
##        print self.who(_f, a), 'done in', bttime() - t
##        if bttime() - t > 1.0:
##            print 'TOO SLOW!'
##            assert False

    def CallAfter(self, callable, *args, **kw):
        """
        Call the specified function after the current and pending event
        handlers have been completed.  This is also good for making GUI
        method calls from non-GUI threads.  Any extra positional or
        keyword args are passed on to the callable when it is called.
        """

        # append (right) and pop (left) are atomic
        self.event_queue.append((callable, args, kw))
        wx.PostEvent(self, self.evt)

    def FutureCall(self, _delay_time, callable, *a, **kw):
        return wx.FutureCall(_delay_time, self.CallAfter, callable, *a, **kw)