Jump to content

zak100

Senior Members
  • Posts

    233
  • Joined

  • Last visited

Everything posted by zak100

  1. Hi, I want to create a GIF file from a text file of 500 x 500 dimensions. I got some help to create the following program which works fine but I have not tried it for text file. Also how I can add the 500 x 500 dimension. My code is: import imageio images = [] filenames = ["one.jpeg", "two.png", "three.png"] for filename in filenames: images.append(imageio.imread(f'./images/{filename}')) #imageio. imageio.mimsave('./images/movie.gif', images) Somebody please guide me. Zulfi.
  2. Hi, Thanks for your useful suggestion and links. Sorry, I could not understand the code provided at: Instead I used a simple solution which i discussed earlier and I found the link also: https://www.bogotobogo.com/python/Multithread/python_multithreading_Identify_Naming_Logging_threads.php My output is this: Though the output is still garbled but not I know which thread is printing what. God bless you guys. My problem is solved now. Zulfi.
  3. Hi, while th1.is_alive(): print("CPU usage:" + str(psutil.Process(pid).cpu_percent(interval=1))) print("thread1 ended") while th2.is_alive(): print("CPU usage:" + str(psutil.Process(pid).cpu_percent(interval=1))) Thanks for your response. Right now its showing CPU usage for only one thread, I have to modify the above code to see for which code its printing CPU usage. Yes, its possible to add the values of two CPU_percent(..) values. Good idea, but first I want to see using large values whether its printing two cpu usage percentages or just one. I would try and then get back to you people. Zulfi.
  4. Hi, Thanks for your response. Okay let's suppose we want to work on two case: 1) Concurrent Execution, may produce jumbled output 2) Running two threads accessing the data at the same time but their output is separate, i.e. we can avoid the jumbled output which I am seeing right now Also for the both the above cases how can I get a combined CPU Usage ? <Have you tried some other online sources or books? . > Not in this case but for CPU usage I did a search. But I would do it now. Thanks. Zulfi.
  5. Hi, My code for selection sort, It prints 3 CPU usage statements: 2 after display random numbers and one after sorting. import random import psutil import os import threading class SSSort: arr = [] # class var access by className.arr def __init__(self, n): self.n = n def generate1000_5digit_RandomNum(self): for i in range(self.n): num = random.randint(10000, 99999) SSSort.arr.append(num) for j in range(self.n): print("random {0}".format(SSSort.arr[j])) def cpu_util_info(self):#make sure different than posted k= self.n arr2 = SSSort.arr[:] def find_the_min_element(k, i, arr2): min = i for j in range(i+1, k): if (arr2[j] < arr2[min]): min = j return min def selectionSort(k, arr2): for i in range(self.n): min = find_the_min_element(k, i, arr2) # swap min and ith element of array temp = arr2[min] arr2[min]= arr2[i] arr2[i] =temp for i in enumerate(arr2): print(i) pid = os.getpid() # current process print("start thread") psutil.Process(pid).cpu_percent(interval=0) th = threading.Thread(target=selectionSort, args=(k, arr2)) th.start() while th.is_alive(): print("CPU usage:" + str(psutil.Process(pid).cpu_percent(interval=1))) print("all done") def main(self): self.generate1000_5digit_RandomNum() self.cpu_util_info() print("memory usage= ", psutil.Process(os.getpid()).memory_info()[0]) print("disk usage= ", psutil.disk_usage('/')) print("virtual mem= ", psutil.virtual_memory().available) print("rss= ", psutil.Process(os.getpid()).memory_info().rss) if __name__ == "__main__": objSSSort = SSSort(10000) objSSSort.main() The output is: Note for 5000, I am getting 2 spu percentages one after random and one after sorting. The above is for 10000 numbers. Thanks for your cooperation. Zulfi.
  6. Hi, I have two sorting programs. I want to concurrently execute them. I have written the following code using help from this forum: pid = os.getpid() # current process print("start thread") psutil.Process(pid).cpu_percent(interval=0) th1 = threading.Thread(target=BubbleSort, args=(k, arr2)) th2 = threading.Thread(target=selectionSort, args=(k, arr2)) th1.start() th2.start() while th1.is_alive(): print("CPU usage:" + str(psutil.Process(pid).cpu_percent(interval=1))) print("thread1 ended") while th2.is_alive(): print("CPU usage:" + str(psutil.Process(pid).cpu_percent(interval=1))) print("thread2 ended") print("all done") Following is the output. This clearly shows that the execution of two threads is overlapping . Initially thread 1 starts prints the sorted list and then control goes to thread2 which prints first 4 numbers and then the control again goes to thread1 which prints the message " thread 1 ended". I want to run the thread 1 to completion and then to start the thread2. In Java we have "synchronize" command. Please guide me how to achieve mutual exclusion in Python. The complete code is: import random import psutil import os import threading class SSSort: arr = [] # class var access by className.arr def __init__(self, n): self.n = n def generate1000_5digit_RandomNum(self): for i in range(self.n): num = random.randint(10000, 99999) SSSort.arr.append(num) for j in range(self.n): print("random {0}".format(SSSort.arr[j])) def cpu_util_info(self): # make sure different than posted k = self.n arr2 = SSSort.arr[:] def find_the_min_element(k, i, arr2): min = i for j in range(i + 1, k): if (arr2[j] < arr2[min]): min = j return min def BubbleSort(k, arr2): for i in arr2: for j in range(len(arr2) - 1): if (arr2[j] > arr2[j + 1]): temp = arr2[j] arr2[j] = arr2[j + 1] arr2[j + 1] = temp for i in enumerate(arr2): print(i) def find_the_min_element(k, i, arr2): min = i for j in range(i+1, k): if (arr2[j] < arr2[min]): min = j return min def selectionSort(k, arr2): for i in range(self.n): min = find_the_min_element(k, i, arr2) # swap min and ith element of array temp = arr2[min] arr2[min]= arr2[i] arr2[i] =temp for i in enumerate(arr2): print(i) pid = os.getpid() # current process print("start thread") psutil.Process(pid).cpu_percent(interval=0) th1 = threading.Thread(target=BubbleSort, args=(k, arr2)) th2 = threading.Thread(target=selectionSort, args=(k, arr2)) th1.start() th2.start() while th1.is_alive(): print("CPU usage:" + str(psutil.Process(pid).cpu_percent(interval=1))) print("thread1 ended") while th2.is_alive(): print("CPU usage:" + str(psutil.Process(pid).cpu_percent(interval=1))) print("thread2 ended") print("all done") def main(self): self.generate1000_5digit_RandomNum() self.cpu_util_info() if __name__ == "__main__": objSSSort = SSSort(10) objSSSort.main() Zulfi. Somebody please guide me, how to solve this problem. Zulfi.
  7. Hi, Thanks for your reply. I am thinking of using the 2nd approach also. But right now I want to correct the cpu usage message problem with the first approach. Please guide me. I want to understand what is my error? I ran the code for 5000 values but still I got multiple values for cpu usages: Though at the end it printed me the single value: Thanks for your cooperation. Zulfi.
  8. Hi, Thanks for your interest in solving my problem. Through your guidance, I am able to print cpu_percent. Of course I have to use 10,000 integer numbers. Now the problem is w.r.t printing of cpu_percent twice as I discussed in post#24. Please guide me why the code is printing cpu_percent() message twice. May God bless you with great success. Zulfi.
  9. Hi, Thanks a lot for correcting my code. God bless you. I want to now focus why I am getting several CPU usage messages. I just want two messages. One message at the start i.e. when I start the measuring of CPU utilization and one at the end i.e. message stating that the sorting completed and for the entire completed process, the total CPU utilization is this much. This is what you were doing. Right now my code is working in the following way: i.e. it is printing the CPU usage after generating random numbers. This happens when the task is assigned to the thread and "th.start()" is executed and the thread enters the while loop. It might be possible that this line: print("CPU usage:" + str(psutil.Process(pid).cpu_percent(interval=1))) is being executed by both the main thread and the new thread. Please guide me. You have pointed out some very good issues. I surely want to focus on them but right now my objective is to make my program behave in the same way as yours is working. But at this point I want to focus on cpu utilization. Kindly guide me. Zulfi.
  10. Hi, Thanks, your trick succeeded. I have to make changes in the code. I have used inner function. Sorting code got disturbed. Its showing multiple output for cpu utlization. Somebody please correct my code. import random import psutil import os import threading class SSSort: arr = [] # class var access by className.arr def __init__(self, n): self.n = n def generate1000_5digit_RandomNum(self): for i in range(self.n): num = random.randint(10000, 99999) SSSort.arr.append(num) for j in range(self.n): print("random {0}".format(SSSort.arr[j])) def cpu_util_info(self): k= self.n arr2 = SSSort.arr[:] def find_the_min_element(k, i, arr2): min = i for j in range(i+1, k): if (arr2[j] < arr2[min]): min = j return min def selectionSort(k, arr2): for i in range(self.n): min = find_the_min_element(k, i, arr2) # swap min and ith element of array temp = arr2[min] arr2[min]= arr2[i] arr2[i] =temp for i in enumerate(SSSort.arr): print(i) pid = os.getpid() # current process print("start thread") psutil.Process(pid).cpu_percent(interval=0) th = threading.Thread(target=selectionSort, args=(k, arr2)) th.start() while th.is_alive(): print("CPU usage:" + str(psutil.Process(pid).cpu_percent(interval=1))) print("all done") def main(self): self.generate1000_5digit_RandomNum() self.cpu_util_info() if __name__ == "__main__": objSSSort = SSSort(20) objSSSort.main() I have two problems: 1) Multiple out for CPU Utilization statement 2) Sorting is not correct. Somebody please correct my code. Its showing multiple cpu utlization and sorting is not correct. Zulfi.
  11. Hi, Thanks for your program. It looks that the key is creating another thread. I also tried with large values but did not create a separate thread. (interval=0) not important. They wait for input. Do you mean that the blocking call stops the execution of all processes and then returns the cpu time for a particular process? Zulfi.
  12. Hi, Thanks for your interest in my problem. I have not explicitly created any thread. I have used the cpu-percent before and after psutil but I am getting zero in both the cases. Somebody please guide me. Zulfi. Hi, I am trying the selection with 1 million integers but my system hangs up. I am using following following command: if __name__ == "__main__": objSSSort = SSSort(1000000) objSSSort.main() In the above code, I am trying to run my selection sort program with 1000000 integers How to solve this problem in Python? Is there any size limitation with integers in python? Zulfi.
  13. Hi and Good Morning, CPU utilization information is still zero. I tried with the sorting code: def systeminfo(self): print("memory usage=", psutil.Process(os.getpid()).memory_info()[0]) print(psutil.disk_usage('/')) print(psutil.Process(os.getpid()).cpu_percent(interval=1)) print("virtual mem= ", psutil.virtual_memory().available) # print(psutil.Process(os.getpid()).memory_info().pfaults) print("rss=", psutil.Process(os.getpid()).memory_info().rss) self.selectionSort() print(psutil.Process(os.getpid()).cpu_percent(interval=1)) def main(self): self.generate1000_5digit_RandomNum() self.systeminfo() if __name__ == "__main__": objSSSort = SSSort(1000) objSSSort.main() I tried sorting with 1000 elements. and at other place: Somebody please guide me. Zulfi.
  14. Hi, Thanks for your cooperation, I am still getting zero for cpu usage: import psutil import os #import memory_usage print("memory usage=", psutil.Process(os.getpid()).memory_info()[0]) print(psutil.disk_usage('/')) print (psutil.Process(os.getpid()).cpu_percent(interval=1)) print("virtual mem= ",psutil.virtual_memory().available) #print(psutil.Process(os.getpid()).memory_info().pfaults) print("rss=", psutil.Process(os.getpid()).memory_info().rss) print (psutil.Process(os.getpid()).cpu_percent(interval=1)) Please guide me how to get some non-zero for cpu utilization. For page faults, following link says: https://readthedocs.org/projects/giamptest/downloads/pdf/latest/ linix, its shared. I am working on linix. Somebody please guide me about page faults and memory usage. Zulfi.
  15. Hi, print(psutil.disk_usage('/')) I got following answer: Is the above okay, can I find it for a process? because I have to compare two processes. print(psutil.Process(os.getpid()).cpu_percent(interval=1) But its giving: 0.0 why? Please guide me. For the following two I am getting attribute error: print(psutil.Process(os.getpid()).virtual_memory().available)//no attribute print(psutil.Process(os.getpid()).memory_info().pfaults)//no aatribute Somebody please guide me. Zulfi.
  16. Hi, Memory part worked, now I am getting error with disk usage: import psutil import os print(psutil.Process(os.getpid()).disk_usage('/').percent) Error is: I found the following link: https://pypi.org/project/psutil/ Somebody please guide me. Zulfi.
  17. Hi, I am trying to display system information in Python. I am now getting following error: My program is below: import psutil import os class Counting_SysInfo: def __init__(self, i): self.i = i def Print_Numbers(self): for i in range(50): print (i) def system_info(self): # return the memory usage in MB self.Print_Numbers() process = psutil.Process(os.getpid()) mem = process.get_memory_info()[0] / float(2 ** 30) # memory in GB cpu = process.cpu_percent() / psutil.cpu_count() disk = process.disk_usage(part.mountpoint).percent # https://github.com/giampaolo/psutil/blob/master/scripts/disk_usage.py vMem = int(process.virtual_memory().available) print(process.memory_info().rss) # in bytes RSS = process.memory_info().rss # https://stackoverflow.com/questions/17990227/whats-the-unit-of-rss-in-psutil-process-get-memory-info pageFaults = process.memory_info().pfaults # https://readthedocs.org/projects/giamptest/downloads/pdf/latest/ print("mem= ", mem, "cpu= ", cpu, 'disk= ',disk, "VMem= ", vMem, "RSS= ", RSS, "page faults=", pageFaults ) def main(self): self.system_info() if __name__ == "__main__": obj_Counting_SysInfo = Counting_SysInfo(10) obj_Counting_SysInfo.main() Somebody please guide me. Zulfi.
  18. Hi, I am reading the following paper: Toward Practical Secure Stable Matching: https://encrypto.de/papers/RSSSK17.pdf. According to the paper: and at other place: Somebody please provide me an example for this model i.e reducing from multiparty data into 2-party data. A very good link is: https://crypto.stackexchange.com/questions/11372/xor-secret-sharing-scheme Zulfi.
  19. Hi, Thanks a lot for solving my problem. God bless you. Zulfi.
  20. Hi, I am trying to write a program for selection sort. I got an example program: https://www.geeksforgeeks.org/selection-sort/ import random class SSSort: arr = [] # class var access by className.arr def __init__(self, n): self.n = n def generate1000_5digit_RandomNum(self): for i in range(self.n): num = random.randint(10000, 99999) SSSort.arr.append(num) for j in range(self.n): print("random {0}".format(SSSort.arr[j])) def find_the_min_element(self, i): min = i for j in range(i+1, self.n-1): if (SSSort.arr[j] < SSSort.arr[min]): min = j return SSSort.arr[min] def selectionSort(self): for i in range(self.n-1): min = self.find_the_min_element(i) #swap min and ith element of array temp = min min = SSSort.arr[i] SSSort.arr[i] = temp for i in enumerate(SSSort.arr): print(i) def main(self): self.generate1000_5digit_RandomNum() self.selectionSort() if __name__ == "__main__": objSSSort = SSSort(20) objSSSort.main() The output of the program is: I think I am facing problem in the swapping. Somebody please solve my problem. Zulfi.
  21. Hi @fiveWorlds and @Strange, Thanks for helping me. I tried cutting and then pasting on notepad and then pasting it back on the pycharm but sorry it did not work. However when I clicked the bulb it asked me to convert the tabs into spaces and this worked. Thanks a lot. Zulfi.
  22. Hi, I have got the following program: class TestIndexInArray: def __init__(self, arr): ''' @param arr: array to be tested ''' self.arr = arr def printItemsIndex(self): ''' Prints the index of each item in a given array ''' # message to tell us when the function was called print(f'Print indexes for {self.arr}:') for i in self.arr: # find the index of i in arr itemIndex = self.arr.index(i) print(itemIndex) print('\n') def findItemAtIndex(self, index): ''' @param index: refers to the location of the item in the array ''' print(f'Item at {index} in {self.arr}:') try: print(self.arr[index]) except IndexError: print(f'> error: index at {index} does not exist') print('\n') if __name__ =="__main__": def header(num): print(f'Test #{num}\n' + '='*40 + '\n') header(1) arr_one = TestIndexInArray([4, 9, 12, 3, 5, 24]) arr_one.printItemsIndex() arr_one.findItemAtIndex(0) arr_one.findItemAtIndex(2) arr_one.findItemAtIndex(5) arr_one.findItemAtIndex(6) header(2) arr_two = TestIndexInArray([]) arr_two.printItemsIndex() arr_two.findItemAtIndex(0) I am getting following errors: Process finished with exit code 1 Somebody please guide me. Zulfi.
  23. Hi, Thanks for guidance. I am able to solve this problem. God bless you. Zulfi.
  24. Hi, I am storing the element in the array. Why index out of range? I am creating an unsized array. How to store elements in the array? Please guide me. Zulfi.
  25. Hi, I am getting some problem with 'n'. Following is my code: import random class TestRandom: arr=[ ] def __init__(self, n): self.n = n def generate1000_5digit_RandomNum(self, n): for i in range(n): num = random.randint(10000, 99999) TestRandom.arr[i] = num for i in range(n): print(TestRandom.arr[i]) def main(self, n): self.generate1000_5digit_RandomNum(n) if __name__ == "__main__": objTestRandom = TestRandom(20) objTestRandom.main(20) I am getting following error: Somebody please guide me. Zulfi.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.