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,6 +12,7 @@ def print_message(msg):
|
|||||||
sleep(1)
|
sleep(1)
|
||||||
print("{}-{}: {}".format(os.getpid(), cp().name, msg))
|
print("{}-{}: {}".format(os.getpid(), cp().name, msg))
|
||||||
|
|
||||||
|
def main():
|
||||||
processes = []
|
processes = []
|
||||||
|
|
||||||
# creating process
|
# creating process
|
||||||
@@ -29,3 +30,6 @@ 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))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
# creating process from a pool
|
# creating process from a pool
|
||||||
with Pool(3) as proc:
|
with Pool(3) as proc:
|
||||||
proc.map(print_message, ["Orange", "Apple", "Banana",
|
proc.map(print_message, ["Orange", "Apple", "Banana",
|
||||||
"Grapes","Pears"])
|
"Grapes","Pears"])
|
||||||
|
|
||||||
print("Exiting the main process")
|
print("Exiting the main process")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
@@ -10,6 +10,7 @@ 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]
|
||||||
|
|
||||||
@@ -26,3 +27,6 @@ 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,6 +9,7 @@ def insert_data (dict1, code, subject):
|
|||||||
def output(dict1):
|
def output(dict1):
|
||||||
print("Dictionary data: ", dict1)
|
print("Dictionary data: ", dict1)
|
||||||
|
|
||||||
|
def main():
|
||||||
with multiprocessing.Manager() as mgr:
|
with multiprocessing.Manager() as mgr:
|
||||||
# create a dictionary in the server process
|
# create a dictionary in the server process
|
||||||
mydict = mgr.dict({100: "Maths", 200: "Science"})
|
mydict = mgr.dict({100: "Maths", 200: "Science"})
|
||||||
@@ -27,3 +28,6 @@ with multiprocessing.Manager() as mgr:
|
|||||||
p3.join()
|
p3.join()
|
||||||
|
|
||||||
print("Exiting the main process")
|
print("Exiting the main process")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
@@ -11,6 +11,7 @@ def output(myqueue):
|
|||||||
while not myqueue.empty():
|
while not myqueue.empty():
|
||||||
print(myqueue.get())
|
print(myqueue.get())
|
||||||
|
|
||||||
|
def main():
|
||||||
mylist = [2, 5, 7]
|
mylist = [2, 5, 7]
|
||||||
myqueue = Queue()
|
myqueue = Queue()
|
||||||
|
|
||||||
@@ -24,3 +25,6 @@ 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,6 +15,8 @@ def myreceiver(r_conn):
|
|||||||
break
|
break
|
||||||
print("Received message : ", msg)
|
print("Received message : ", msg)
|
||||||
r_conn.close()
|
r_conn.close()
|
||||||
|
|
||||||
|
def main():
|
||||||
sender_conn, receiver_conn= Pipe()
|
sender_conn, receiver_conn= Pipe()
|
||||||
|
|
||||||
p1 = Process(target=mysender, args=(sender_conn, ))
|
p1 = Process(target=mysender, args=(sender_conn, ))
|
||||||
@@ -27,3 +29,6 @@ p1.join()
|
|||||||
p2.join()
|
p2.join()
|
||||||
|
|
||||||
print("Exiting the main process")
|
print("Exiting the main process")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
@@ -9,6 +9,7 @@ def printme (lock, msg):
|
|||||||
finally:
|
finally:
|
||||||
lock.release()
|
lock.release()
|
||||||
|
|
||||||
|
def main():
|
||||||
with Pool(3) as proc:
|
with Pool(3) as proc:
|
||||||
lock = Manager().Lock()
|
lock = Manager().Lock()
|
||||||
func = partial(printme,lock)
|
func = partial(printme,lock)
|
||||||
@@ -16,3 +17,6 @@ with Pool(3) as proc:
|
|||||||
"Grapes","Pears"])
|
"Grapes","Pears"])
|
||||||
|
|
||||||
print("Exiting the main process")
|
print("Exiting the main process")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user