473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Urllib2/threading errors under Cygwin


Hi,

I have a problem with using urllib2 with threading module under Cygwin.

$ cygcheck -cd cygwin python
Cygwin Package Information
Package Version
cygwin 1.5.5-1
python 2.3.2-1

Here is minimal app where I can reproduce errors:

--- MtUrllib2Test.py -------------------------------------------------
#!/usr/bin/env python

import urllib2
import threading
import sys
import time
def FetchPage():
# time.sleep(3)
# return
opener = urllib2.build_opener()
urlFile = opener.open( 'http://google.com/' )
pageData = urlFile.read()
def IncCounterAndPrint( count=[0] ):
count[0] += 1
print count[0]
# sys.stdout.flush()
def Main():
noOfThreads = 1
for unused in range(noOfThreads):
thread = threading.Thread( target=FetchPage, args=() )
thread.start()
# time.sleep(0.2)
IncCounterAndPrint()

while(threading.activeCount()>1):
IncCounterAndPrint()
time.sleep(0.5)
IncCounterAndPrint()
if __name__ == "__main__":
Main()
--- MtUrllib2Test.py -------------------------------------------------

0. Simple case. Here everything looks ok:

$ python MtUrllib2Test.py
1
2
3
4
5
1. First error.

$ python MtUrllib2Test.py | tee out.txt
3
4
5

Leading prints has been eaten somewhere. Uncommenting disabled code in
ANY of the functions does make output correct, but none of the solutions
looks good for me:

a) IncCounterAndPrint() - sys.stdout.flush()
As I understand if stdout is not console then output gets buffered (i.e.
it's not flushed automatically). Adding a flush call does make output
good, but this looks like a kludge for me, not a real fix. I am writing
to stdout from only one thread, so everything should be fine without
calling flush, shouldn't it?

b) Main() - time.sleep(0.2)
Adding a little sleep after starting thread does make output correct
too. For me it looks like race condition either in urllib2 or in
cygwin. Or am I completely off here?

c) FetchPage() - time.sleep(3), return
Disabling calls to urllib2 does make problem go away, too.
2. Second error.

If I increase number of threads:
noOfThreads = 20
and run this prog (you may need to run it several times, or rise number
of threads more to reproduce), then sometimes it does fail this way :

$ python MtUrllib2Test.py | tee out.txt
4 [win] python 1744 Winmain: Cannot register window class
C:\cygwin\bin\python2.3.exe: *** WFSO failed, Win32 error 6

or hangs this way:

$ python MtUrllib2Test.py | tee out.txt
243 [win] python 1696 Winmain: Cannot register window class
520 [win] python 1696 Winmain: Cannot register window class

Can anyone help me on those two?

Best regards,
Jacek.

Jul 18 '05 #1
0 1507

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

Similar topics

0
by: Joonas Paalasmaa | last post by:
Hi, When compiling Sketch's streamfilter C extension the errors below are raised during linking. What could cause the errors? (Python 2.3, MinGw 1.1 with GCC 2.95.3-6, Windows 98) Here are...
1
by: Matthew Wilson | last post by:
I am writing a script to check on my router's external IP address. My ISP refreshes my IP very often and I use dyndns for the hostname for my computer. My Netgear mr814 router has a webserver that...
2
by: AdSR | last post by:
Hello, Nothing relevant found on Google for this, so I'm asking here. I wrote a short script that launches several threads (parallel download using urllib.urlretrieve in this case). Sometimes...
2
by: Jay Davis | last post by:
We have a lot of simple code using 'urllib', that basically just goes out and opens and read()'s some http url. Very basic, ten line scripts, and they work fine with urllib. We want to use...
3
by: Anand Pillai | last post by:
I recently noted that urllib2.urlopen(...) for http:// urls does not make an explicit call to close the underlying HTTPConnection socket once the data from the socket is read. This might not be...
3
by: Savagesmc | last post by:
Problem Background: I am working on a thin wrapper for the pthreads interface on linux, and have encountered frustrating behavior. The idea is to have a "PosixThread" class that any class can...
0
by: Dan M | last post by:
I'm writing a system monitor script that needs to load web pages. I'm using urllib2.urlopen to get the pages, and I'm attempting to set the timeout value using socket.defaulttimeout. ...
6
by: robean | last post by:
Hi everyone, I have a question about using urllib2. I like urllib2 better than urllib at least in part because it has more elaborate support for handling errors: there is built in support for...
3
by: konstantin | last post by:
Hi, I wonder if there is a safe way to download page with urllib2. I've constructed following method to catch all possible exceptions. def retrieve(url): user_agent = 'Mozilla/4.0...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.