473,785 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3273
On Jun 25, 4:11*pm, Roopesh <roopesh....@gm ail.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....@gm ail.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
3024
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* file, not for the *remaining* data to be written. It happens both under 2.3 and 2.2.3. Any ideas?
0
1412
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 last): File "<pyshell#20>", line 1, in ?
1
7776
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 ---------------------------------------------------------------------------- ----------------- import re, os, time, shutil os.chdir(os.environ+os.environ+"\\Desktop") DESKTOP = os.listdir(os.environ+os.environ+"\\Desktop")
7
6714
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 works fine except when its at the copy line(s). Nothing gets copied, it crashes at the first open() line. Before using open() I made it the lazy way first and wrote system("cp
5
9633
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 all the contents of f2 will be created and thrown away. In the case of two StringIO objects, this means there's a point when the contents is held in memory three times.
0
2023
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 called gramps (http://www.gramps-project.org) for MAC OS X.
3
9119
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 with extension. --- method 1: # File extension utility. import os
3
1634
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 = upload_file.file.read(8192) if not data: break total_data += data
6
6030
by: elake | last post by:
I found this thread about a pst file in Windows being locked and I am having the same issue. http://groups.google.com/group/comp.lang.python/browse_thread/thread/d3dee5550b6d3652/ed00977acf62484f?lnk=gst&q=%27copying+locked+files%27&rnum=1 The problem is that I have a script that can find the pst files on every machine in my network and back them up to a server for safe keeping. The problem is that when Outlook is running it locks the...
0
10346
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8982
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7504
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.