473,378 Members | 1,623 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,378 software developers and data experts.

Problems with file IO in a thread, and shutil

Hi,

I have a multithreaded application. There are two threads, T1 and T2.
Suppose that there are two folders A, B. Thread T1 fetches data from
network and creates files in folder A and after creating each file, it
moves the file to folder B, using shutil.move().

Thread T2, takes files from folder B and processes it.
Note: Only the thread T1 has access to the files in folder A.

psuedo code
=========
class T1(thread):
def run():
self.download()

def download():
data = from_network
filename = os.path.join ( "A", "file1")

f = open ( filename, "w")
for d in data:
f.write ( d + "\n" )
f.flush()
f.close()

shutil.move(os.path.join ( "A", "file1"), os.path.join("B",
"file1"))
class T2(thread):
run()
process(listdir(os.path.join("B")))

All the files has similar contents. But in some cases(very rare), when
thread T1 tries to move the newly created file from folder A to folder
B, an exception occurs (on shutil.move()).

The exception is WindowsError(32, 'The process cannot access the file
because it is being used by another process'). I looked inside the
shutil.move. What I found was due to this WindowsError, the usual
os.rename() inside shutil.move() fails and the file is copied using
copy2(src, dst). Finally os.unlink() also failed. (So though copying
occurred, deleting the source file failed)

I am not getting why this error comes. Has anyone faced similar
situation? Please help me.

Thanks and Regards
Roopesh
Jun 27 '08 #1
4 3236
On Jun 25, 4:11*pm, Roopesh <roopesh....@gmail.comwrote:
Hi,

I have a multithreaded application. There are two threads, T1 and T2.
Suppose that there are two folders A, B. *Thread T1 fetches data from
network and creates files in folder A and after creating each file, it
moves the file to folder B, using shutil.move().

Thread T2, takes files from folder B and processes it.
Note: Only the thread T1 has access to the files in folder A.

psuedo code
=========
class T1(thread):
* *def run():
* * *self.download()

* def download():
* * *data = from_network
* * *filename = os.path.join ( "A", "file1")

* * *f = open ( filename, "w")
* * *for d in data:
* * * * f.write ( d + "\n" )
* * *f.flush()
* * *f.close()

* * shutil.move(os.path.join ( "A", "file1"), *os.path.join("B",
"file1"))

class T2(thread):
* run()
* * * process(listdir(os.path.join("B")))

All the files has similar contents. But in some cases(very rare), when
thread T1 tries to move the newly created file from folder A to folder
B, an exception occurs (on shutil.move()).

The exception is WindowsError(32, 'The process cannot access the file
because it is being used by another process'). I looked inside the
shutil.move. What I found was due to this WindowsError, the usual
os.rename() inside shutil.move() fails and the file is copied using
copy2(src, dst). Finally os.unlink() also failed. (So though copying
occurred, deleting the source file failed)

I am not getting why this error comes. Has anyone faced similar
situation? Please help me.

Thanks and Regards
Roopesh
Do you have any anti-virus/anti-spyware software installed? (If not,
why not? :-))

It might be that your anti-virus/anti-spyware software is seeing the
new file appear and scanning it. If that happens just when
shutil.move() attempts to move it then the move will fail. You could
have your code wait for a while and then try again.
Jun 27 '08 #2
Thanks for the reply. I did testing in a clean system, were anti virus/
spyware is not installed. It still gave this problem, in say 1 out of
1000 cases.

By any chance would it be possible that the Windows OS has not
completed writing to the file even after file.flush() and file.close()
is called?

Thanks
Roopesh
Jun 27 '08 #3
On Jun 26, 8:06*am, Roopesh <roopesh....@gmail.comwrote:
Thanks for the reply. I did testing in a clean system, were anti virus/
spyware is not installed. It still gave this problem, in say 1 out of
1000 cases.

By any chance would it be possible that the Windows OS has not
completed writing to the file even after file.flush() and file.close()
is called?

Thanks
Roopesh
Maybe it's a Windows service, eg the indexing service or generating
thumbnails. Anyway, retrying after a delay would still be worth it.
Jun 27 '08 #4
Maybe it's a Windows service, eg the indexing service or generating
thumbnails. Anyway, retrying after a delay would still be worth it.
Yes, this thing works :-) Thanks a lot.

Thanks
Roopesh
Jun 28 '08 #5

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

Similar topics

7
by: Patrick Useldinger | last post by:
Hi, I think I found a bug in the write method of file objects. It seems as if before writing each block, a check was done in order to verifiy that there is enough space left for the *whole*...
0
by: hamido | last post by:
Hi Why this statement will generate errors shutil.copy2("c:\hamido.sql", "c:\ali1.ali") shutil.copy2('c:\hamido.sql', 'c:\ali1.ali') ------------------- Error Traceback (most recent call...
1
by: bmgz | last post by:
I am have made a simple script that moves all desktop clutter (ie files that are not *.lnk) to a specified folder eg. c:\myhome\mydocs\desktopdebris\2003-12-16 ...
7
by: Eric Belanger | last post by:
Hi, Before posting I made sure I upgraded from 2.3.3 to 2.3.4, but the problem is still here. Ive created a little script which, at the end of it, deals with copying two files. All the script...
5
by: Thomas Lotze | last post by:
Hi, another question: What's the most efficient way of copying data between two file-like objects? f1.write(f2.read()) doesn't seem to me as efficient as it might be, as a string containing...
0
by: Richard Taylor | last post by:
User-Agent: OSXnews 2.07 Xref: number1.nntp.dca.giganews.com comp.lang.python:437315 Hi I am trying to use py2app (http://undefined.org/python/) to package a gnome-python application...
3
by: Phoe6 | last post by:
Hi all, I had a filesystem crash and when I retrieved the data back the files had random names without extension. I decided to write a script to determine the file extension and create a newfile...
3
by: OriginalBrownster | last post by:
I am currently uploading a file from a users computer to the file system on my server using python, just reading the file and writing the binaries. total_data=' ' while True: data =...
6
by: elake | last post by:
I found this thread about a pst file in Windows being locked and I am having the same issue. ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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...

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.