Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pcp.py #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions pcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,26 @@ def get_num_cores(self):
"""

# Python 2.6+
try:
if sys.version_info.minor > 6:
import multiprocessing
return multiprocessing.cpu_count()
except (ImportError, NotImplementedError):
pass

# POSIX
try:
# this returns int on my computer (Linux)
self.cores = int(os.sysconf('SC_NPROCESSORS_ONLN'))

if self.cores > 0:
return self.cores

except (AttributeError, ValueError):
pass

# Windows
try:
self.cores = int(os.environ['NUMBER_OF_PROCESSORS'])

if self.cores > 0:
return self.cores
except (KeyError, ValueError):
pass
# You can use get on a dictionary to avoid key errors and set a 'default' value
# If this is your last check before failing, default to 1 (as you obviously need one, or raise an error)
self.cores = int(os.environ.get('NUMBER_OF_PROCESSORS', 1))
return self.cores


def check_dir(self, dir_path):
"""
Expand Down Expand Up @@ -176,13 +173,14 @@ def workers(self, queue, source, dest, cpus=0):
"""
Create threads and copy
"""

startTime = time.time()

self.cores = self.get_num_cores()
if cpus == 0:
cpus = self.cores / 3
elif cpus > self.cores:
print "You can set a max of {0} CPU's to use.".format(self.cores)
exit()
cpus = self.cores / 3
print "Too many cores selected, setting to {0} cores.".format(cpus)

print "Starting the copy with {0} threads.".format(cpus)

Expand All @@ -193,6 +191,12 @@ def workers(self, queue, source, dest, cpus=0):

queue.join()

# consider adding up the total file size being transferred and comparing the transfer rates
# depending on the number of cores you use - you may find something interesting....
timeDelta = time.time() - startTime
print "Copy job completed in {0} seconds".format(timeDelta)


if __name__ == '__main__':
pcp = pcp()
args = pcp.cli_params()
Expand Down