Question

In: Computer Science

The Pool class of the multiprocessing Python module. There is a defined run method to perform...

The Pool class of the multiprocessing Python module. There is a defined run method to perform the tasks. Create a pool object of the Pool class of a specific number of CPUs your system has by passing a number of tasks you have. Start each task within the pool object by calling the map instance method, and pass the run function and the list of tasks as an argument.Hint: os.walk() generates the file names in a directory tree by walking the tree either top-down or bottom-up. This is used to traverse the file system in Python.

Multisync.py

#!/usr/bin/env python3
from multiprocessing import Pool
def run(task):
  # Do something with task here
    print("Handling {}".format(task))
if __name__ == "__main__":
  tasks = ['task1', 'task2', 'task3']
  # Create a pool of specific number of CPUs
  p = Pool(len(tasks))
  # Start each task within the pool
  p.map(run, tasks)

The hierarchy of the subfolders of /data/prod, data is from different projects (e.g., , beta, gamma, kappa) and they're independent of each other. You have to use multiprocessing and subprocess module methods to sync the data from /data/prod to /data/prod_backup folder.

Try applying multiprocessing, which takes advantage of the idle CPU cores for parallel processing. Here, you have to use multiprocessing and subprocess module methods to sync the data from /data/prod to /data/prod_backup folder.

Hint: os.walk() generates the file names in a directory tree by walking the tree either top-down or bottom-up. This is used to traverse the file system in Python.

Dailysync.py

#!/usr/bin/env python
import subprocess
src = "/data/prod/"
dest = "/data/prod_backup/"
subprocess.call(["rsync", "-arq", src, dest])

Solutions

Expert Solution

Ans:

'''

The above code can use multiprocessing and the subprocess module to do the following syncing task ( to backup data)

'''

#!/usr/bin/env python3

from multiprocessing import Pool

import subprocess

import os

from pathlib import Path, PureWindowsPath

def run(task):

# Do something with task here

#print("Handling {}".format(task))

src = "data/prod/"

dest = "data/prod_backup/"

subprocess.call(["rsync", "-arq", src, dest])

print(task)


if __name__ == "__main__":

print("Before multiprocess synchronisation: ")

print("Directory structure and files to be synchronised: ")

for root, dirs, files in os.walk("data"):

print("root dir path:",root)

print("directories inside:",dirs)

print("files inside:",files)

cores = os.cpu_count()

print("cores that can be utilised :",cores)

# Create a pool of specific number of CPUs

p = Pool()

tasks = []

# Start each task within the pool

for i in range(1,cores+1):

tasks.append("task{}".format(i))

p.map(run, tasks)

print("After multiprocess synchronisation: ")

print("Directory structure and files to be synchronised: ")

for root, dirs, files in os.walk("data"):

print("root dir path:",root)

print("directories inside:",dirs)

print("files inside:",files)

# BEFORE RUNNING THE SCRIPT THE DIRECTORY STRUCTURE WAS:

# AFTER RUNNING THE SCRIPT a 'prod_backup' folder was created and all files were synced inside it

# REQUIRED OUTPUT:

# PLEASE DO LIKE AND UPVOTE IF THIS WAS HELPFUL!

# THANK YOU SO MUCH IN ADVANCE!


Related Solutions

In python- Create a class defined for Regression. Class attributes are data points for x, y,...
In python- Create a class defined for Regression. Class attributes are data points for x, y, the slope and the intercept for the regression line. Define an instance method to find the regression line parameters (slope and intercept). Plot all data points on the graph. Plot the regression line on the same plot.
I have defined the class. Rectangle in Python. How do I define a class Canvas with...
I have defined the class. Rectangle in Python. How do I define a class Canvas with the following description: Class Canvas represents a collection of Rectangles. It has 8 methods. In addition, to the constructor (i.e. __init__ method) and two methods that override python's object methods (and make your class user friendly as suggested by the test cases), your class should contain 5 more methods: add_one_rectangle, count_same_color, total_perimeter, min_enclosing_rectangle, and common_point.
The abstract class SayHello and the static method process are defined as followed: class SayHello {...
The abstract class SayHello and the static method process are defined as followed: class SayHello {             private String greeting; SayHello(   )             {                         greeting = “Hello guys”;             } SayHello( String wts )             {                         greeting = wts;             }             void printGreeting( ); } static void process( SayHello obj ) {             obj.printGreeting(   ); } Write the statements to instantiate an object (with its instance variable initialized with the string “Bonjour mes amis” ) of the anonymous class that extends this abstract class by using the...
Python Create a move function that is only defined in the base class called Objects. The...
Python Create a move function that is only defined in the base class called Objects. The move function will take two parameters x,y and will also return the updated x,y parameters.
Python: What are the defintions of the three method below for a class Date? class Date(object):...
Python: What are the defintions of the three method below for a class Date? class Date(object): "Represents a Calendar date"    def __init__(self, day=0, month=0, year=0): "Initialize" pass def __str__(self): "Return a printable string representing the date: m/d/y" pass    def before(self, other): "Is this date before that?"
Given an unordered list (defined using the UnorderedList class above), transform it into a Python list.When...
Given an unordered list (defined using the UnorderedList class above), transform it into a Python list.When the input list is empty, the output Python list should be empty as well.Example: When the input unordered list is [1,2,3], the output Python list should be [1,2,3] . Use the following codes: class Node: def __init__(self, data): self.data = data self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self, data): self.data = data def setNext(self, node): self.next = node...
PYTHON Suppose you need to import a single class named GeneTools from a module named bioinformatics,...
PYTHON Suppose you need to import a single class named GeneTools from a module named bioinformatics, in Python. Which of the following represents the correct use of an import statement? import GeneTools from bioinformatics from bioinformatics import GeneTools from bioinformatics import * import GeneTools
Write a Class called Module with the following attributes: module code, module name, list of lecturers...
Write a Class called Module with the following attributes: module code, module name, list of lecturers for the module (some modules may have more than one lecturer – we only want to store their names), number of lecture hours, and module description. Create a parameterised (with parameters for all of the class attributes) and a non-parameterised constructor, and have the accessor and mutator methods for each attribute including a toString method. Write a class Student with the following attributes: student...
By using Python: a. Make a class called Book. The __init__() method for Book should store...
By using Python: a. Make a class called Book. The __init__() method for Book should store two attributes: a title and an author. Make a method called book_info() that prints these two pieces of information, and a method called book_available() that prints a message indicating that the book is available in the library. b. Create an instance called book1 from your class. Print the two attributes individually, and then call both methods. c. Create three different instances from the class,...
For python Write a new method for the Fraction class called mixed() which returns a string...
For python Write a new method for the Fraction class called mixed() which returns a string that writes out the Fraction in mixed number form. Here are some examples: f1 = Fraction(1, 2) print(f1.mixed()) # should print "1/2" f2 = Fraction(3, 2) print(f2.mixed()) # should return "1 and 1/2" f3 = Fraction(5, 1) print(f3.mixed()) # should return "5" def gcd(m, n): while m % n != 0: oldm = m oldn = n m = oldn n = oldm %...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT