I have several threads each of which is changing working directories and occasionally creating new ones in specific working directories, copying/moving files etc in these directories. Think e.g.:
def thread1: while True: os.chdir('dir') os.mkdir('newdir') os.system('mv *.png newdir/') do something def thread2: while True: os.chdir('another-dir') os.mkdir('another-newdir') os.system('mv *.png another-newdir/') do something
I've read that chdir, mkdir functions are not specific to threads but global. What is a way to accomplish this? I can try to use absolute paths but is that the best solution?
preguntado el 05 de mayo de 13 a las 20:05
The working directory is an information of the MODO DE PREPARACIÓN, hence all threads share the same working directory. You debe: work with absolute paths if you want to use multiple threads.
You can obtain absolute paths quite easily using the
An other thing that you may consider is using python's standard library to do the copying instead of calling external processes.
Ver por ejemplo:
The Current Working Directory at OS level usually is process specific, not per thread. (Linux: see dejar de compartir)
Python internal file operations mostly can be re-written to use a
os.path.join() y evitar
When this is not possible, e.g. for unchangeable code, for
RExec kind of sandbox executions or so, then you could provide a virtual
open functions etc. which do path adjustment behind the scenes.
For calling external programs, you can provide a
cwd=... argument in subprocess.Popen() calls and friends.
Instead of using multiple threads, you could as well use multiple processes which solves the issue.
I have backup script that work on multiple disks, and by using a process per disk, I can make sure disk utilization stays at 100% on all disks.
There are a variety of other libraries to manage process pools in similar ways to thread pools, my favorite is the competidor paquete.