Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8131237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:01:51+00:00 2026-06-06T09:01:51+00:00

I have a simple Python script that uses a signal handler for Ctl-C. If

  • 0

I have a simple Python script that uses a signal handler for Ctl-C. If the program completes normally, the end time is passed into the “print_results” function. I wanted the print_results function to have an optional parameter that, if not passed, simply gets the current “now” time. But when I call it from the signal handler, it does not get the correct time.

Here is my simplified, but reproducible, program:

import sys
import signal
import urllib2
import urllib
import datetime
import time
import getopt,sys

def signal_handler(signal, frame):
    print_results()
    sys.exit(0)

def print_results(ended=datetime.datetime.now()):
    print "\nEnded at ",ended
    print "Total time: ",(ended - startTime)
    print "Finished ",numIterations," iterations, received ",totalRecords," records"

    numIterations = 0
    maxIterations = 8
    delaySecs = 3
    totalRecords = 0

    # set up signal handler
    signal.signal(signal.SIGINT, signal_handler)

    startTime = datetime.datetime.now()

    print "Starting at ",time.asctime(time.localtime())

    while (numIterations < maxIterations):

        iterStartTime = datetime.datetime.now()

        numIterations += 1

        print "Iteration: ",numIterations

        # sleep if necessary

        if delaySecs > 0:
            time.sleep(delaySecs)

        iterEndTime = datetime.datetime.now()

        print "Iteration time: ",(iterEndTime - iterStartTime)

    endTime = datetime.datetime.now()

    print "Ended at ",time.asctime(time.localtime())
    print "Total test time: ",(endTime - startTime)

    print_results(endTime)

Here is what happens when I type Ctl-C

$ python test.py                                                           
Starting at  Fri Jun 15 08:28:15 2012
Iteration:  1
Iteration time:  0:00:03.003101
Iteration:  2
Iteration time:  0:00:03.003105
Iteration:  3
^C
Ended at  2012-06-15 08:28:15.766496
Total time:  -1 day, 23:59:59.999964
Finished  3  iterations, received  0  records

It seems like that when print_results is called with no arguments that the ‘ended’
value is not being interpreted correctly as a datetime object. But since Python
does not have a way to cast (as far as I can tell), I cannot tell what is wrong.

Thanks in advance,

Mitch

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:01:53+00:00Added an answer on June 6, 2026 at 9:01 am

    The problem you are having is that you are evaluating the function in the parameter. This means that ended=datetime.datetime.now() takes the value of the time when this is being parsed, not when it is called. What you should do is something like this:

    def print_results(ended=None):
        if ended is None:
            ended = datetime.datetime.now()
        ...
    

    Here there is a really good explanation of why this happens: “Least Astonishment” in Python: The Mutable Default Argument

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Python script that uses the socket module to send a
I have the following simple python test script that uses Suds to call a
I have a very simple python script that should scan a text file, which
I have a python script on a vps that I run a simple command
I have a simple python program that I'd like to daemonize. Since the point
I have a simple Python script that receives username and password as arguments, but
I have a rather simple Python script that contains a function call like f(var,
I have a simple python script that updates that statuses of justin.tv streams in
I have a python script that runs on windows and uses win32 extensions and
I have a simple Python script that I want to stop executing if a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.