Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73388

Re: Under what kind of situation, time.sleep(n) would sleep much longer than n seconds?

References <713f4189-5ffe-492d-9c7d-447bb4b4928a@googlegroups.com> <mailman.11114.1403087670.18130.python-list@python.org> <4d7c474a-b88e-4dea-b74c-ed34f4969310@googlegroups.com>
Date 2014-06-19 09:38 +1000
Subject Re: Under what kind of situation, time.sleep(n) would sleep much longer than n seconds?
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.11128.1403134697.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Jun 19, 2014 at 1:24 AM, crow <wentlv@gmail.com> wrote:
> Here is a sample code that can reproduce this issue, you need to wait for it to run for a while.
> ******************
> import time
> import threading
> import wx
>
> def sleep():
>     while True:
>         t = time.time()
>         time.sleep(1)
>         print "Actually sleep:", time.time() - t
>
>
> t1 = threading.Thread(target=sleep)
> t1.start()
>
> app =wx.App(False)
> frame = wx.Frame(None, title="test")
> frame.Show(True)
> app.MainLoop()

Okay... you're adding two important complications that you hadn't
mentioned originally: Python threads, and a GUI main loop. I could
explain the impact that each of these might have on time.sleep(), but
given your other recent questions, I'm going to say this instead:

Don't be afraid of the console.

When you write Python code, start by assuming that it will be run in
the standard "glass teletype" console. One thread, simple imperative
code, no GUI event loop, keep it simple. Later on, you can add fancy
features, but start by getting your main program code working in a
simpler environment.

In a lot of cases, a simple single-threaded console program is
actually all you need. You can transport that to other environments
easily (maybe run it under a web framework with minimal changes), and
it'll run perfectly on pretty much any build of Python, without any
fiddling around. And you spend so little time and code on your UI that
way; any half-decently-usable GUI will probably take you a good bit of
effort to code, but a good console UI just requires this:

something = raw_input("Enter something: ")
print("Result: "+result)

(Or, better: Upgrade to Python 3, and drop the "raw_" prefix. You get
other benefits too.)

Play with GUIs later, if at all. Start by getting the guts of your code right.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Under what kind of situation, time.sleep(n) would sleep much longer than n seconds? crow <wentlv@gmail.com> - 2014-06-18 02:52 -0700
  Re: Under what kind of situation, time.sleep(n) would sleep much longer than n seconds? Marko Rauhamaa <marko@pacujo.net> - 2014-06-18 13:30 +0300
  Re: Under what kind of situation, time.sleep(n) would sleep much longer than n seconds? Chris Angelico <rosuav@gmail.com> - 2014-06-18 20:34 +1000
    Re: Under what kind of situation, time.sleep(n) would sleep much longer than n seconds? crow <wentlv@gmail.com> - 2014-06-18 08:24 -0700
      Re: Under what kind of situation, time.sleep(n) would sleep much longer than n seconds? Chris Angelico <rosuav@gmail.com> - 2014-06-19 09:38 +1000
        Re: Under what kind of situation, time.sleep(n) would sleep much longer than n seconds? crow <wentlv@gmail.com> - 2014-06-18 18:41 -0700
          Re: Under what kind of situation, time.sleep(n) would sleep much longer than n seconds? Chris Angelico <rosuav@gmail.com> - 2014-06-19 13:58 +1000
        Re: Under what kind of situation, time.sleep(n) would sleep much longer than n seconds? Christian Gollwitzer <auriocus@gmx.de> - 2014-06-19 09:18 +0200
          Re: Under what kind of situation, time.sleep(n) would sleep much longer than n seconds? Chris Angelico <rosuav@gmail.com> - 2014-06-19 17:42 +1000
            Re: Under what kind of situation, time.sleep(n) would sleep much longer than n seconds? Marko Rauhamaa <marko@pacujo.net> - 2014-06-19 12:20 +0300
            Re: Under what kind of situation, time.sleep(n) would sleep much longer than n seconds? Christian Gollwitzer <auriocus@gmx.de> - 2014-06-19 19:17 +0200
              Re: Under what kind of situation, time.sleep(n) would sleep much longer than n seconds? Chris Angelico <rosuav@gmail.com> - 2014-06-20 08:57 +1000
          Re: Under what kind of situation, time.sleep(n) would sleep much longer than n seconds? Terry Reedy <tjreedy@udel.edu> - 2014-06-19 22:33 -0400
          Re: Under what kind of situation, time.sleep(n) would sleep much longer than n seconds? Chris Angelico <rosuav@gmail.com> - 2014-06-20 12:54 +1000

csiph-web