The following should be in a file named multithreadpy a Crea

The following should be in a file named multithread.py

a) Create a function named byte_counter that has a byte value and a filename as parameters and displays the number of times that byte appears in the file

b) Suppose now that the input file is extremely large.
Multiple readers in a file is acceptable, so modify your solution to create multiple
threads that count in different parts of the file such that each thread is responsible for
a certain part of the file.

Collate the data from each thread and provide the correct total.

In the \"if __name__ == \"__main__\" part of your file, use the timeit module to time both the single- threaded new multi-threaded solutions. Then print statements that say something about the difference in performance, if any.



2. The following should be in a file named modprodcons.py

Instead of a producer thread and a consumer thread, change the code for prodcons.py (from PythonMultithreading.pptx) so that you have any number of consumer threads (a thread pool) which can process or consume more than one item from the Queue at any given moment.

second question code:

The file should be opened in binary mode (\'rb\'). A call to read(somesize) will return a bytes object of length somesize. Helpful 1. To convert a string to bytes #string data easy data encode #bytes easy data b easy #bytes 2. To convert bytes to a String data b easy #bytes decode() #string data b easy 3. To convert an integer n to bytes: n to bytes(numbytes,byteorder) numbytes is the number of bytes to use (should be 1 for integers between 0 and 127, inclusive) byteorder is the byte ordering of the processor (big big endian ttle little endian\', use \'big\' until you get an error!) 4. Code to create a file containing of sz random bytes def create big file(fname,sz). with open (fname,\'Wb\') as f for i n range(size): f.write (random randint(0,127) to bytes(1,\'big\'))) 5. To get the size of a file with name pth: os path.filesize(pth) 6. To set the read position for a file to k bytes from the start, file.seek(k)

Solution

#!/usr/bin/env python

from random import randint
from time import sleep
from Queue import Queue
from myThread import MyThread

def writeQ(queue):
print \'producing object for alphabetic character...\',
queue.put(\'xxx\', 1)
print \"size now\", queue.qsize()

def readQ(queue):
val = queue.get(1)
print \'consumed object from alphabetic character... size now\', \\
queue.qsize()

def writer(queue, loops):
for i in range(loops):
writeQ(queue)
sleep(randint(1, 3))

def reader(queue, loops):
for i in range(loops):
readQ(queue)
sleep(randint(2, 5))

funcs = [writer, reader]
nfuncs = range(len(funcs))
def main():
nloops = randint(2, 5)
q = Queue(32)

threads = []
for i in nfuncs:
t = MyThread(funcs[i], (q, nloops), \\
funcs[i].__name__)
threads.append(t)

for i in nfuncs:
threads[i].start()

for i in nfuncs:
threads[i].join()

print \'all DONE\'

if __name__ == \'__main__\':
main()

The following should be in a file named multithread.py a) Create a function named byte_counter that has a byte value and a filename as parameters and displays t
The following should be in a file named multithread.py a) Create a function named byte_counter that has a byte value and a filename as parameters and displays t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site