Share the Knowledge
RSS icon Home icon
  • Random Quotes

    Posted on September 30th, 2010 webmaster No comments         Print Print

    I’m currently reading Chris Guillebeau’s The Art of Non-Conformity and I really liked the quotes he put throughout the book.  I often paused for a few seconds after reading each one to think about it.

    I thought it might be a good idea to have random quotes appear on this blog so I searched the WordPress site for plugins and found this Quotes Collection plugin that can do exactly what I want (and more).  It lets you create a collection of quotes that can then be displayed on a page or on the sidebar.  If you look at the sidebar right now the first thing you’ll see is a random quote.  When you refresh the page or go to another page you’ll notice the sidebar will display a new random quote.

    I only have a few quotes entered right now but I’ll make sure to keep this updated whenever I find a new one that I like.

    Read the rest of this entry »

  • Deadlock issue when using Python’s subprocess module

    Posted on September 24th, 2010 webmaster No comments         Print Print

    I ran into an issue yesterday when calling an external Java application from Python using the subprocess module.  The Java application loaded but appeared to have stopped executing after loading its properties files (last entry in the log file is after loading the properties file).

    My Python code looked something like this:

    import subprocess
    
    javaApplication = subprocess.Popen(["java", "-jar", "application.jar"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
    # The wait() function is actually not a good idea to use
    # when using stdout=PIPE or stderr=PIPE. Use the communicate()
    # function instead, so this line is actually not needed.
    javaApplication.wait()
    
    (stdout, stderr) = javaApplication.communicate()
    

    If I don’t use the stdout=subprocess.PIPE and stderr=subprocess.PIPE the Java application executes properly, but I need to capture the standard output and standard error from the Java application.  Another thing I noticed is if I disable the console appender in log4j (logging service for Java), the Java applications executes normally as well.  So there appears to be some conflict between the Java and the Python processes.

    Since I don’t really need log4j to output to the console this would’ve solved my problem but it was still bothering me so I played with it a little bit more.  I enabled the console appender in log4j again but this time I commented out javaApplication.wait().  After doing that the Java app executed normally.  I thought I needed javaApplication.wait() to capture the standard out/error but it turned out it wasn’t necessary as the communicate() function took care of it.

    I looked back at the subprocess module’s documentation and there was actually a mention there that the wait() function can cause a deadlock when using stdout=PIPE or stderr=PIPE.  I guess I should really pay more attention to a module’s documentation a bit more next time before using that module :) .

  • How to change the TIME-WAIT delay and the highest source port number that can be assigned in Windows XP or Windows Server 2003

    Posted on September 15th, 2010 webmaster No comments         Print Print

    We ran into an issue last night where a web server we use for testing wasn’t able to open anymore connections. It turned out that our web application was creating too many connections too fast and Windows ran out of port numbers to allocate.

    By default, Windows XP and Windows Server 2003 (not sure about other versions of Windows) wait 4 minutes before releasing sockets in TIME-WAIT state (RFC 793) and the highest port number it can allocate is 5000.  There are registry keys you can add to get around these limitations (in our case this is just a temporary workaround of course, we obviously need to fix our code :D ).

    1. Open the Registry Editor (Start -> Run -> regedit).
    2. Go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters.
    3. Add a new DWORD value:
      1. To set the TIME-WAIT delay, create a new DWORD value called TCPTimedWaitDelay and assign a decimal value from 30 to 300 (the number of seconds to wait before closing the socket and releasing the port number assigned to it).
      2. To increase the the highest source port number that Windows can allocate, create a new DWORD value called MaxUserPorts and assign a decimal value up to 65,535 (a user port is normally above 1024 so don’t set this too low, most likely you’ll need it over 5000 anyway if you’re reading this).
    4. Close the Registry Editor and reboot your machine.

    Source: http://msdn.microsoft.com/en-us/library/ms819739.aspx