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 Speedy Shopper with GUI.py #3

Open
wants to merge 3 commits 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
112 changes: 46 additions & 66 deletions Speedy Shopper with GUI.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import time
import os
import wx
from multiprocessing import cpu_count, Pipe, Process, current_process
#import wx

grocery_list = []
grocery_list = ['bread', 'wine', 'gadgets']
time_completed = 0
aisle1 = ["aisle1", "bread", "cereal", "cookies"]
aisle2 = ["aisle2", "butter", "milk", "cream"]
aisles = [aisle1, aisle2]
aisles_rev = [aisle2, aisle1]
requested_aisles = []
requested_aisles_dict = {}


"""
class Home(wx.Frame):
global grocery_list

Expand Down Expand Up @@ -71,78 +67,62 @@ def append_grocery(self, event):
def search(self, event):
search(grocery_list)
wx.Exit() # Exits so that the multi-processing can begin. Need to find a work around but this be a temp fix

"""
aisles = {}
aisles_full = {}
def initialize(file):
global aisles
global aisles_full
data = open(file).read().splitlines()

aisle_designator = 0

for line in data:
if line == '':
aisle_designator += 1
elif line == 'hbc':
aisle_designator = 'hbc'
elif line == 'front':
aisle_designator = 'front'
elif line == 'back':
aisle_designator = 'back'
elif line == 'pharmacy':
aisle_designator = 'pharmacy'
else:
aisles[line.lower()] = 'aisle ' + str(aisle_designator)
aisles_full = aisles

class Scan:
global requested_aisles

def reg(self, item, conn):
global requested_aisles
print("Running: " + current_process().name + " [PID: " + str(os.getpid()) + "]")
for aisle in aisles:
if item in aisle:
print(aisle[0] + " From [reg]")
if aisle[0] not in requested_aisles:
conn.send(aisle[0]) # Pipes info out of sub-process back into main stream
conn.close()
print("Finished: " + current_process().name)

def rev(self, item, conn):
print("Running: " + current_process().name + " [PID: " + str(os.getpid()) + "]")
for aisle in aisles_rev:
if item in aisle:
print(aisle[0] + " From [rev]")
if aisle[0] not in requested_aisles:
conn.send(aisle[0]) # Pipes info out of sub-process back into main stream
conn.close()
print("Finished: " + current_process().name)

def sort(self, item):
print("Running: " + current_process().name + " [PID: " + str(os.getpid()) + "]")
for aisle in aisles:
if item in aisle:
print(aisle[0] + " From search sort")
if aisle[0] not in requested_aisles:
requested_aisles.append(aisle[0])
print("Finished: " + current_process().name)


scan = Scan()
cpu_quantity = cpu_count() // 2

global requested_aisles_dict
global aisles_full

def create_processes(item): # Multi-Process scan threaded to different cpu's
for i in range(cpu_quantity):
globals()['parent_conn%s' % i], globals()['child_conn%s' % i] = Pipe()
for i in range(cpu_quantity): # Determine whether or not scan will be regular or reversed
def reg(self, item):
print("Running: " + "[PID: " + str(os.getpid()) + "]")
for grocery in aisles_full:
if grocery == item:
print(grocery)
print(aisles_full[grocery] + " From [reg]")
if aisles_full[grocery] not in requested_aisles_dict.keys():
requested_aisles_dict[aisles_full[grocery]] = item

if i % 2 == 0:
globals()['process%s' % i] = Process(target=scan.reg, args=(item, globals()['child_conn%s' % i],)) # Start regular search process
else:
globals()['process%s' % i] = Process(target=scan.rev, args=(item, globals()['child_conn%s' % i],)) # reverse search process
print("Finished: " + "[PID: " + str(os.getpid()) + "]")

for i in range(cpu_quantity):
globals()['process%s' % i].start() # Initialize all processes and distribute to cpu cores
temp_rcv = globals()['parent_conn%s' % i].recv()
if temp_rcv not in requested_aisles:
requested_aisles.append(temp_rcv)

for i in range(cpu_quantity):
globals()['process%s' % i].join() # Hold program running until all process jobs are complete
scan = Scan()


def search(items):
global time_completed
start_time = time.time()
for i in items:
create_processes(i)
scan.reg(i)
time_completed = time.time() - start_time


if __name__ == '__main__':
app = wx.App()
"""app = wx.App()
frame = Home(None).Show()
app.MainLoop()
print("Amount of CPU's: " + str(cpu_quantity))
print(requested_aisles) # Print what item from grocery list is in the aisle
app.MainLoop()"""
initialize('item_locations.txt')
search(grocery_list)
print(requested_aisles_dict) # Print what item from grocery list is in the aisle
print("Time Completed: " + str(time_completed))