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)
|
sleep(1)
|
||||||
print("{}-{}: {}".format(os.getpid(), cp().name, msg))
|
print("{}-{}: {}".format(os.getpid(), cp().name, msg))
|
||||||
|
|
||||||
processes = []
|
def main():
|
||||||
|
processes = []
|
||||||
|
|
||||||
# creating process
|
# creating process
|
||||||
processes.append(Process(target=print_hello, name="Process 1"))
|
processes.append(Process(target=print_hello, name="Process 1"))
|
||||||
processes.append(Process(target=print_hello, name="Process 2"))
|
processes.append(Process(target=print_hello, name="Process 2"))
|
||||||
processes.append(Process(target=print_message,
|
processes.append(Process(target=print_message,
|
||||||
args=["Good morning"], name="Process 3"))
|
args=["Good morning"], name="Process 3"))
|
||||||
|
|
||||||
# start the process
|
# start the process
|
||||||
for p in processes:
|
for p in processes:
|
||||||
p.start()
|
p.start()
|
||||||
|
|
||||||
# wait till all are done
|
# wait till all are done
|
||||||
for p in processes:
|
for p in processes:
|
||||||
p.join()
|
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)
|
sleep(1)
|
||||||
print("{}-{}: {}".format(os.getpid(), cp().name, msg))
|
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
|
inc_list[index] = num + 1
|
||||||
sum.value = sum.value + inc_list[index]
|
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,
|
p = Process(target=inc_sum_list,
|
||||||
args=(mylist, inc_list, sum))
|
args=(mylist, inc_list, sum))
|
||||||
|
|
||||||
p.start()
|
p.start()
|
||||||
p.join()
|
p.join()
|
||||||
print("incremented list: ", list(inc_list))
|
print("incremented list: ", list(inc_list))
|
||||||
print("sum of inc list: ", sum.value)
|
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):
|
def output(dict1):
|
||||||
print("Dictionary data: ", dict1)
|
print("Dictionary data: ", dict1)
|
||||||
|
|
||||||
with multiprocessing.Manager() as mgr:
|
def main():
|
||||||
# create a dictionary in the server process
|
with multiprocessing.Manager() as mgr:
|
||||||
mydict = mgr.dict({100: "Maths", 200: "Science"})
|
# create a dictionary in the server process
|
||||||
|
mydict = mgr.dict({100: "Maths", 200: "Science"})
|
||||||
|
|
||||||
p1 = Process(target=insert_data, args=(mydict, 300, "English"))
|
p1 = Process(target=insert_data, args=(mydict, 300, "English"))
|
||||||
p2 = Process(target=insert_data, args=(mydict, 400, "French"))
|
p2 = Process(target=insert_data, args=(mydict, 400, "French"))
|
||||||
p3 = Process(target=output, args=(mydict,))
|
p3 = Process(target=output, args=(mydict,))
|
||||||
|
|
||||||
p1.start()
|
p1.start()
|
||||||
p2.start()
|
p2.start()
|
||||||
|
|
||||||
p1.join()
|
p1.join()
|
||||||
p2.join()
|
p2.join()
|
||||||
|
|
||||||
p3.start()
|
p3.start()
|
||||||
p3.join()
|
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():
|
while not myqueue.empty():
|
||||||
print(myqueue.get())
|
print(myqueue.get())
|
||||||
|
|
||||||
mylist = [2, 5, 7]
|
def main():
|
||||||
myqueue = Queue()
|
mylist = [2, 5, 7]
|
||||||
|
myqueue = Queue()
|
||||||
|
|
||||||
p1 = Process(target=copy_data, args=(mylist, myqueue))
|
p1 = Process(target=copy_data, args=(mylist, myqueue))
|
||||||
p2 = Process(target=output, args=(myqueue,))
|
p2 = Process(target=output, args=(myqueue,))
|
||||||
|
|
||||||
p1.start()
|
p1.start()
|
||||||
p1.join()
|
p1.join()
|
||||||
p2.start()
|
p2.start()
|
||||||
p2.join()
|
p2.join()
|
||||||
|
|
||||||
print("Queue is empty: ",myqueue.empty())
|
print("Queue is empty: ",myqueue.empty())
|
||||||
print("Exiting the main process")
|
print("Exiting the main process")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
@@ -15,15 +15,20 @@ def myreceiver(r_conn):
|
|||||||
break
|
break
|
||||||
print("Received message : ", msg)
|
print("Received message : ", msg)
|
||||||
r_conn.close()
|
r_conn.close()
|
||||||
sender_conn, receiver_conn= Pipe()
|
|
||||||
|
|
||||||
p1 = Process(target=mysender, args=(sender_conn, ))
|
def main():
|
||||||
p2 = Process(target=myreceiver, args=(receiver_conn,))
|
sender_conn, receiver_conn= Pipe()
|
||||||
|
|
||||||
p1.start()
|
p1 = Process(target=mysender, args=(sender_conn, ))
|
||||||
p2.start()
|
p2 = Process(target=myreceiver, args=(receiver_conn,))
|
||||||
|
|
||||||
p1.join()
|
p1.start()
|
||||||
p2.join()
|
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:
|
finally:
|
||||||
lock.release()
|
lock.release()
|
||||||
|
|
||||||
with Pool(3) as proc:
|
def main():
|
||||||
lock = Manager().Lock()
|
with Pool(3) as proc:
|
||||||
func = partial(printme,lock)
|
lock = Manager().Lock()
|
||||||
proc.map(func, ["Orange", "Apple", "Banana",
|
func = partial(printme,lock)
|
||||||
"Grapes","Pears"])
|
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