473,603 Members | 2,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: better scheduler with correct sleep times

sokol wrote:
What was a surprise to me was that python sched.py makes the same
mistake as I did in my first version.
The sched module is *not* designed for multithreading. It
assumes that the thread waiting for events is the only one
putting events into the queue, so it's impossible for an
event to get scheduled while in the midst of a sleep. It
also doesn't take any measures to protect its data structures
from concurrent access.

The documentation could make this clearer, especially since
it confusingly talks about "allowing other threads to run".

--
Greg
Oct 21 '08 #1
5 1751
On Oct 21, 2:19*am, greg <g...@cosc.cant erbury.ac.nzwro te:
sokol wrote:
What was a surprise to me was that python sched.py makes the same
mistake as I did in my first version.

The sched module is *not* designed for multithreading. It
assumes that the thread waiting for events is the only one
putting events into the queue, so it's impossible for an
event to get scheduled while in the midst of a sleep. It
also doesn't take any measures to protect its data structures
from concurrent access.

The documentation could make this clearer, especially since
it confusingly talks about "allowing other threads to run".
I find that hard to believe. Scheduler in single threaded
application is useless (well, you can correct me because
right now I can't come up with an example). Also, the
scheduler runs inside a loop. How do you suppose to
run other code while the loop is executing? Remember, all
you have is a single thread. The consequence of this is
that the only way to insert something new inside a queue
is by doing it from scheduled function. Furthermore,
if scheduler is single threaded, why does is
check if the top event has changed after sleep period?

What I can agree is that python sched as it is (not
designed for multithreading) is quite useless.

--
Tvrtko
Oct 21 '08 #2
On Oct 21, 4:58*am, sokol <tvrtko.sokolov ...@gmail.comwr ote:
On Oct 21, 2:19*am, greg <g...@cosc.cant erbury.ac.nzwro te:
sokol wrote:
What was a surprise to me was that python sched.py makes the same
mistake as I did in my first version.
The sched module is *not* designed for multithreading. It
assumes that the thread waiting for events is the only one
putting events into the queue, so it's impossible for an
event to get scheduled while in the midst of a sleep. It
also doesn't take any measures to protect its data structures
from concurrent access.
The documentation could make this clearer, especially since
it confusingly talks about "allowing other threads to run".

I find that hard to believe. Scheduler in single threaded
application is useless (well, you can correct me because
right now I can't come up with an example). Also, the
scheduler runs inside a loop. How do you suppose to
run other code while the loop is executing? Remember, all
you have is a single thread. The consequence of this is
that the only way to insert something new inside a queue
is by doing it from scheduled function.
Yeah, that does happen.

I often used this sort of thing (pre-Python days, so a long time ago)
in single-threaded, real-time simulations.

Don't know mcuh about the sched module, but time scheduling in general
doesn't need multithreading, not one bit.
Carl Banks

Oct 21 '08 #3
sokol wrote:
Also, the
scheduler runs inside a loop. How do you suppose to
run other code while the loop is executing?
The sleep function could be doing a select with a
timeout on some other source of events, such as a
socket or a gui input event stream. One possible
response to such an event is to schedule another
event. There's not so much need for that nowadays,
since most gui libraries provide a way of scheduling
timed events as part of their built-in event loop,
but you might want to use something like this in
a server that deals with network connections.

Another possible use is discrete-event simulation,
where the "sleep" function doesn't physically sleep
but just advances a simulated time, and all events
(other than the first one that starts everything off)
are scheduled by callbacks for other events.

So while its uses are rather specialized, I wouldn't
say it's useless. The main problem is that its nature
needs to be much more clearly spelled out in the
docs -- it's something of an attractive nuisance the
way it is.

--
Greg
Oct 22 '08 #4
On 2008-10-21, sokol <tv************ ***@gmail.comwr ote:
On Oct 21, 2:19*am, greg <g...@cosc.cant erbury.ac.nzwro te:
>sokol wrote:
What was a surprise to me was that python sched.py makes the same
mistake as I did in my first version.

The sched module is *not* designed for multithreading. It
assumes that the thread waiting for events is the only one
putting events into the queue, so it's impossible for an
event to get scheduled while in the midst of a sleep. It
also doesn't take any measures to protect its data structures
from concurrent access.

The documentation could make this clearer, especially since
it confusingly talks about "allowing other threads to run".

I find that hard to believe. Scheduler in single threaded
application is useless (well, you can correct me because
right now I can't come up with an example).
Imagine your environment doesn't provide any kind of multithreading
support. Couldn't you write an interactive game, a FTP server, a
messaging system? Programmers have done that for years. The fact that
you *can* write such things using multiple threads doesn't mean that't
the only way to do that, nor the best one. It's like `fork`, the Unix
programmer's hammer: every problem becomes a nail.
Also, the
scheduler runs inside a loop. How do you suppose to
run other code while the loop is executing? Remember, all
you have is a single thread. The consequence of this is
that the only way to insert something new inside a queue
is by doing it from scheduled function. Furthermore,
if scheduler is single threaded, why does is
check if the top event has changed after sleep period?

What I can agree is that python sched as it is (not
designed for multithreading) is quite useless.
The sched module (and mutex too, BTW) exists right from the beginning
of Python, ages before multithreading support were added to the
language. The algorithm hasn't changed in years; I wouldn't say it's
useless, it's just not suitable for the kind of scheduling one usually
wants to do in a multithreaded environment.
The latest release (2.6) contains this warning::

In multi-threaded environments, the scheduler class has
limitations with respect to thread-safety, inability to insert a new
task before the one currently pending in a running scheduler, and
holding up the main thread until the event queue is empty. Instead, the
preferred approach is to use the threading.Timer class instead.

--
Gabriel Genellina

Oct 22 '08 #5
On Oct 22, 2:28*am, greg <g...@cosc.cant erbury.ac.nzwro te:
sokol wrote:
Also, the
scheduler runs inside a loop. How do you suppose to
run other code while the loop is executing?

The sleep function could be doing a select with a
timeout on some other source of events, such as a
socket or a gui input event stream. One possible
response to such an event is to schedule another
event. There's not so much need for that nowadays,
since most gui libraries provide a way of scheduling
timed events as part of their built-in event loop,
but you might want to use something like this in
a server that deals with network connections.

Another possible use is discrete-event simulation,
where the "sleep" function doesn't physically sleep
but just advances a simulated time, and all events
(other than the first one that starts everything off)
are scheduled by callbacks for other events.

So while its uses are rather specialized, I wouldn't
say it's useless. The main problem is that its nature
needs to be much more clearly spelled out in the
docs -- it's something of an attractive nuisance the
way it is.

--
Greg
I see. The delayfunc is user defined function so it
doesn't have to sleep at all. If you are creative
enough, you can use this scheduler in many ways.

--
Tvrtko
Oct 23 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2572
by: project2501 | last post by:
i'm doing some benchmarking and python is certainly fast enough (the time.time resolution is more than good enough). however, i am using the threading module to implement worker trheads to hit a server with varying levels of workload. hwoever, my result graphs (of server response time) are very flat and these flat graphs are higher with the number of threads. this suggests to me that the barrier i'm hitting is the python thread
12
18612
by: Conrad | last post by:
Greetings, Q: Is there some way to gracefully suspend a python app for a second or so then resume? I could write the classic basic dumb loop-tenzillion-times delay, but that seems inelegant, and well, just wrong. By the way, I'm also using wxPython, if that helps any. I need to delay a python program for about a second. Googling around, it looks like most of the timer stuff has to do with thread handling, but my application is not...
11
4065
by: Codemonkey | last post by:
Hi, I am writing an App in .Net that involves some scheduling of tasks. I was wondering if anybody has come accross any components or examples of how to implement a schedule manager like the one in the Task Scheduler for Windows? Basically I'm after something that'll allow me to specify a "recurring" or "once off" schedule with advanced options like "the first monday in the month" etc.
21
18697
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or nanosleep(), depending on platform }
1
2451
by: kmounkhaty | last post by:
Hi Guru, Our HR sql 2000 production server is 800.760 built (SP3a) with 2GB of RAM and 2 CPU hiper threading. This server is very low activity where processor runs average 1-3 during the day. However, in the last two months, I've seen the following message occurred a few times on this server. Error: 17883, Severity: 1, State: 0 The Scheduler 2 appears to be hung. SPID 0, ECID 0, UMS Context
1
5243
by: Nagendra Kumar | last post by:
Hello ALL, I am trying to schdule some of my class methods using sched module of python import sched, time s=sched.scheduler(time.time, time.sleep) event1=s.enter(60, 1, obj.scheduleAbuseAssignment1, ()) event2=s.enter(60, 1, obj.scheduleAbuseAssignment2, ())
1
1025
TRScheel
by: TRScheel | last post by:
Here's a good one. I have spent all day researching this, and I can not for the life of me find a method that will work. All I want to do... is add a scheduled task to the Task Scheduler. I have seen this: http://www.c-sharpcorner.com/UploadFile/ajifocus/AppScheduler05262006074807AM/AppScheduler.aspx which is not what I want. Windows already has a scheduler program in there, I dont need to rewrite it. If I wanted that, I would just...
1
1807
by: Prof. William Battersea | last post by:
I'd like a class method to fire every n seconds. I tried this: class Timed: def.__init__(self): self.t = Timer(3, self.dothing) def.start(self): self.t.start()
3
201
by: Scott David Daniels | last post by:
qvx wrote: The trick is to use Queue's timeout argument to interrupt your sleep when new requests come in. def time_server(commands): '''Process all scheduled operations that arrive on queue commands''' pending = while True:
0
7996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7928
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8273
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3951
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.