wrapping python code in main()
The code will not give error on Windows OS if wrapped inside the main function.
This commit is contained in:
@@ -12,20 +12,24 @@ def print_message(msg):
|
||||
sleep(1)
|
||||
print("{}-{}: {}".format(os.getpid(), cp().name, msg))
|
||||
|
||||
processes = []
|
||||
def main():
|
||||
processes = []
|
||||
|
||||
# creating process
|
||||
processes.append(Process(target=print_hello, name="Process 1"))
|
||||
processes.append(Process(target=print_hello, name="Process 2"))
|
||||
processes.append(Process(target=print_message,
|
||||
args=["Good morning"], name="Process 3"))
|
||||
# creating process
|
||||
processes.append(Process(target=print_hello, name="Process 1"))
|
||||
processes.append(Process(target=print_hello, name="Process 2"))
|
||||
processes.append(Process(target=print_message,
|
||||
args=["Good morning"], name="Process 3"))
|
||||
|
||||
# start the process
|
||||
for p in processes:
|
||||
p.start()
|
||||
# start the process
|
||||
for p in processes:
|
||||
p.start()
|
||||
|
||||
# wait till all are done
|
||||
for p in processes:
|
||||
p.join()
|
||||
# wait till all are done
|
||||
for p in processes:
|
||||
p.join()
|
||||
|
||||
print("Exiting the main process")
|
||||
print("Exiting the main process")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -7,9 +7,14 @@ def print_message(msg):
|
||||
sleep(1)
|
||||
print("{}-{}: {}".format(os.getpid(), cp().name, msg))
|
||||
|
||||
# creating process from a pool
|
||||
with Pool(3) as proc:
|
||||
proc.map(print_message, ["Orange", "Apple", "Banana",
|
||||
"Grapes","Pears"])
|
||||
|
||||
print("Exiting the main process")
|
||||
def main():
|
||||
# creating process from a pool
|
||||
with Pool(3) as proc:
|
||||
proc.map(print_message, ["Orange", "Apple", "Banana",
|
||||
"Grapes","Pears"])
|
||||
|
||||
print("Exiting the main process")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -10,19 +10,23 @@ def inc_sum_list(list, inc_list, sum):
|
||||
inc_list[index] = num + 1
|
||||
sum.value = sum.value + inc_list[index]
|
||||
|
||||
def main():
|
||||
|
||||
mylist = [2, 5, 7]
|
||||
mylist = [2, 5, 7]
|
||||
|
||||
inc_list = multiprocessing.Array('i', 3)
|
||||
inc_list = multiprocessing.Array('i', 3)
|
||||
|
||||
sum = multiprocessing.Value('i')
|
||||
sum = multiprocessing.Value('i')
|
||||
|
||||
p = Process(target=inc_sum_list,
|
||||
args=(mylist, inc_list, sum))
|
||||
p = Process(target=inc_sum_list,
|
||||
args=(mylist, inc_list, sum))
|
||||
|
||||
p.start()
|
||||
p.join()
|
||||
print("incremented list: ", list(inc_list))
|
||||
print("sum of inc list: ", sum.value)
|
||||
p.start()
|
||||
p.join()
|
||||
print("incremented list: ", list(inc_list))
|
||||
print("sum of inc list: ", sum.value)
|
||||
|
||||
print("Exiting the main process")
|
||||
print("Exiting the main process")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -9,21 +9,25 @@ def insert_data (dict1, code, subject):
|
||||
def output(dict1):
|
||||
print("Dictionary data: ", dict1)
|
||||
|
||||
with multiprocessing.Manager() as mgr:
|
||||
# create a dictionary in the server process
|
||||
mydict = mgr.dict({100: "Maths", 200: "Science"})
|
||||
def main():
|
||||
with multiprocessing.Manager() as mgr:
|
||||
# create a dictionary in the server process
|
||||
mydict = mgr.dict({100: "Maths", 200: "Science"})
|
||||
|
||||
p1 = Process(target=insert_data, args=(mydict, 300, "English"))
|
||||
p2 = Process(target=insert_data, args=(mydict, 400, "French"))
|
||||
p3 = Process(target=output, args=(mydict,))
|
||||
p1 = Process(target=insert_data, args=(mydict, 300, "English"))
|
||||
p2 = Process(target=insert_data, args=(mydict, 400, "French"))
|
||||
p3 = Process(target=output, args=(mydict,))
|
||||
|
||||
p1.start()
|
||||
p2.start()
|
||||
p1.start()
|
||||
p2.start()
|
||||
|
||||
p1.join()
|
||||
p2.join()
|
||||
p1.join()
|
||||
p2.join()
|
||||
|
||||
p3.start()
|
||||
p3.join()
|
||||
p3.start()
|
||||
p3.join()
|
||||
|
||||
print("Exiting the main process")
|
||||
print("Exiting the main process")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -11,16 +11,20 @@ def output(myqueue):
|
||||
while not myqueue.empty():
|
||||
print(myqueue.get())
|
||||
|
||||
mylist = [2, 5, 7]
|
||||
myqueue = Queue()
|
||||
def main():
|
||||
mylist = [2, 5, 7]
|
||||
myqueue = Queue()
|
||||
|
||||
p1 = Process(target=copy_data, args=(mylist, myqueue))
|
||||
p2 = Process(target=output, args=(myqueue,))
|
||||
p1 = Process(target=copy_data, args=(mylist, myqueue))
|
||||
p2 = Process(target=output, args=(myqueue,))
|
||||
|
||||
p1.start()
|
||||
p1.join()
|
||||
p2.start()
|
||||
p2.join()
|
||||
p1.start()
|
||||
p1.join()
|
||||
p2.start()
|
||||
p2.join()
|
||||
|
||||
print("Queue is empty: ",myqueue.empty())
|
||||
print("Exiting the main process")
|
||||
print("Queue is empty: ",myqueue.empty())
|
||||
print("Exiting the main process")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -15,15 +15,20 @@ def myreceiver(r_conn):
|
||||
break
|
||||
print("Received message : ", msg)
|
||||
r_conn.close()
|
||||
sender_conn, receiver_conn= Pipe()
|
||||
|
||||
p1 = Process(target=mysender, args=(sender_conn, ))
|
||||
p2 = Process(target=myreceiver, args=(receiver_conn,))
|
||||
def main():
|
||||
sender_conn, receiver_conn= Pipe()
|
||||
|
||||
p1.start()
|
||||
p2.start()
|
||||
p1 = Process(target=mysender, args=(sender_conn, ))
|
||||
p2 = Process(target=myreceiver, args=(receiver_conn,))
|
||||
|
||||
p1.join()
|
||||
p2.join()
|
||||
p1.start()
|
||||
p2.start()
|
||||
|
||||
print("Exiting the main process")
|
||||
p1.join()
|
||||
p2.join()
|
||||
|
||||
print("Exiting the main process")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -9,10 +9,14 @@ def printme (lock, msg):
|
||||
finally:
|
||||
lock.release()
|
||||
|
||||
with Pool(3) as proc:
|
||||
lock = Manager().Lock()
|
||||
func = partial(printme,lock)
|
||||
proc.map(func, ["Orange", "Apple", "Banana",
|
||||
"Grapes","Pears"])
|
||||
def main():
|
||||
with Pool(3) as proc:
|
||||
lock = Manager().Lock()
|
||||
func = partial(printme,lock)
|
||||
proc.map(func, ["Orange", "Apple", "Banana",
|
||||
"Grapes","Pears"])
|
||||
|
||||
print("Exiting the main process")
|
||||
print("Exiting the main process")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user