Re: [Twisted-Python] Run Twisted reactor from a Thread

2015-02-03 Thread dw+twisted-python
If you can, check out Crochet. It wraps up most of the mess involved in
making this work, and vastly simpliies the experience..

http://crochet.readthedocs.org/en/latest/


David

On Tue, Feb 03, 2015 at 03:41:17PM +0100, Abdelhalim Kadi wrote:
> Hello;
> 
> 
> When i run reactor from thread in a synchrone python program, the twisted code
> is never called.
> 
> To resolve this problem, I had to put a sleep.
> 
> def _reactor_thread(self):
> if not self.reactor.running:
> self.reactor.run(installSignalHandlers=0)
> 
> 
> def _start_thread( self ):
> 
> self.client_thread = Thread( target=self._reactor_thread,
>  name="mine" )
> self.client_thread.setDaemon(True)
> self.client_thread.start()
> from time import sleep
> sleep( 0.5 )
> 
> What is the best way to do it, instead of calling sleep?
> 
> 
> Thank you.
> 
> 
> Abdel Halim
> 

> ___
> Twisted-Python mailing list
> Twisted-Python@twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


Re: [Twisted-Python] Scalability of timers

2014-08-10 Thread dw+twisted-python
Hey Tobias,

Individual OS have their own mechanisms for avoiding the kind of waste
you're describing. For example, Linux quite aggressively rounds up the
expiry of certain classes of timer at progressively less granular
intervals the further in the future they're scheduled ("timer
coalescing").

When Twisted wakes, there is no guarantee that that only one timer has
expired by then. In fact under load you would expect the select loop to
always be running (and thus timing out) late, and so each iteration may
process several timers simultaneously.

Twisted will set the select() timeout to the timer due to expire the
earliest. Finding this timer is a constant time operation. There is only
ever one active select() (or select-equivalent) call active at a time.

The Twisted timer implementation internally uses a heap, so scheduling
and expiry are quite efficint O(logN). With 4 billion timers active,
scheduling a new timer in the worst case would require 32 array elements
to be swapped.


On Sun, Aug 10, 2014 at 05:16:51AM -0700, Tobias Oberstein wrote:
> Hi,
> 
> I have a question regarding scalability of timers in Twisted.
> 
> Say I have a massive number of periodic timers (lets say each with period 1s, 
> but all slightly time shifted to each other).
> 
> As far as I understand, timers are implemented ultimately by setting the 
> timeout parameter when calling into OS select/poll/epoll/kqueue.
> 
> If this  is true, then the number of timers scales linearly with the number 
> of syscalls. This can get limiting (the total number of syscalls a Linux box 
> can sustain is a couple of 100k's per second). As more and more timers are 
> setup, the timeout essentially will approach 0. On the upside, timers will 
> fire precisely.
> 
> However, say I am fine with a precision of 1ms.
> 
> Is there a way that limits the syscall rate to 1000/s (given no FD activity 
> happens) _independently_ of the number of timers setup?
> 
> Timers that fall into a certain ms slice would all fire roughly at the same 
> time (still ordered).
> 
> Is that possible?
> 
> Thanks,
> Tobias
> 
> ___
> Twisted-Python mailing list
> Twisted-Python@twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


Re: [Twisted-Python] Scalability of timers

2014-08-10 Thread dw+twisted-python
On Sun, Aug 10, 2014 at 02:51:02PM -0700, Tobias Oberstein wrote:

> But the coalescing you describe would also apply to those implicit
> timers (created from select(timeout = ..) within the Linux kernel)?

It applies to all kernel timers not created by realtime processes.


> This is all fine. But how do I _explicitly_ limit the rate at which
> select() is called to say 1000Hz (at the expense of timer precision)?

> I don't want to let the box hit it's syscall rate limit. Because the
> box will spend a fair amount of resources for context switching all
> the time with to real gain.

>From a reading of http://lwn.net/Articles/296578/, at time of writing
the default select() implementation coalesces sub-second timeouts to
50us boundaries, and this can be adjusted via prctl(PR_SET_TIMERSLACK)
(http://linux.die.net/man/2/prctl) on a per-process basis.

That article is from 2008, and though the relevant kernel code seems to
match the article content, a huge amount of power-efficiency related
changes went into the kernel since that time. My assumption is nowadays
the kernel rounds more aggressively than the default of 50us documented
by that article.

Short answer is yes, you can set the max hz of select(), but in all
likelihood you won't have to. As always, benchmarking and profiling real
code might reveal this to be a non-issue.


David

> 
> Thanks for your hints and patience,
> Tobias
> 
> > 
> > 
> > On Sun, Aug 10, 2014 at 05:16:51AM -0700, Tobias Oberstein wrote:
> > > Hi,
> > >
> > > I have a question regarding scalability of timers in Twisted.
> > >
> > > Say I have a massive number of periodic timers (lets say each with period 
> > > 1s,
> > but all slightly time shifted to each other).
> > >
> > > As far as I understand, timers are implemented ultimately by setting the
> > timeout parameter when calling into OS select/poll/epoll/kqueue.
> > >
> > > If this  is true, then the number of timers scales linearly with the 
> > > number of
> > syscalls. This can get limiting (the total number of syscalls a Linux box 
> > can
> > sustain is a couple of 100k's per second). As more and more timers are 
> > setup,
> > the timeout essentially will approach 0. On the upside, timers will fire
> > precisely.
> > >
> > > However, say I am fine with a precision of 1ms.
> > >
> > > Is there a way that limits the syscall rate to 1000/s (given no FD 
> > > activity
> > happens) _independently_ of the number of timers setup?
> > >
> > > Timers that fall into a certain ms slice would all fire roughly at the 
> > > same time
> > (still ordered).
> > >
> > > Is that possible?
> > >
> > > Thanks,
> > > Tobias
> > >
> > > ___
> > > Twisted-Python mailing list
> > > Twisted-Python@twistedmatrix.com
> > > http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
> > 
> > ___
> > Twisted-Python mailing list
> > Twisted-Python@twistedmatrix.com
> > http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
> 
> ___
> Twisted-Python mailing list
> Twisted-Python@twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


Re: [Twisted-Python] Scalability of timers

2014-08-10 Thread dw+twisted-python
On Sun, Aug 10, 2014 at 03:31:11PM -0700, Tobias Oberstein wrote:

> Thanks a lot for those hints! I will read into this material.

Just a final note.. a single no-fds call to select with a 0 timeout
seems to take around 280ns on my Core 2. Presumably the better
interfaces (e.g. epoll, but not poll) will also take around the same
time.

It's really hard to write even a single Python function that gets
anywhere below 1usec CPU time, and given how function-heavy Twisted is,
I'd be surprised considerations like this factored usefully into a
design at all :)

Adjusting timer coalescing to extreme settings might even worsen your
app's performance, since it'll cause interpreter time and syscalls to
all be compressed around the slack intervals, leaving the CPU idle more
often, rather than running evenly spaced over time. This might produce
less desirable app behavior overall (e.g. it has knock-on effects for
network interface queues, bursts of disk IO/SQL queries, network switch
buffer overruns, or whatever else).


David

___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python