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:
muassif
2021-08-14 14:33:10 +04:00
parent cdf2c7d7fb
commit 3a0dab2f58
7 changed files with 95 additions and 65 deletions
+4
View File
@@ -12,6 +12,7 @@ def print_message(msg):
sleep(1)
print("{}-{}: {}".format(os.getpid(), cp().name, msg))
def main():
processes = []
# creating process
@@ -29,3 +30,6 @@ for p in processes:
p.join()
print("Exiting the main process")
if __name__ == '__main__':
main()
+5
View File
@@ -7,9 +7,14 @@ def print_message(msg):
sleep(1)
print("{}-{}: {}".format(os.getpid(), cp().name, msg))
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()
+4
View File
@@ -10,6 +10,7 @@ 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]
@@ -26,3 +27,6 @@ print("incremented list: ", list(inc_list))
print("sum of inc list: ", sum.value)
print("Exiting the main process")
if __name__ == '__main__':
main()
+4
View File
@@ -9,6 +9,7 @@ def insert_data (dict1, code, subject):
def output(dict1):
print("Dictionary data: ", dict1)
def main():
with multiprocessing.Manager() as mgr:
# create a dictionary in the server process
mydict = mgr.dict({100: "Maths", 200: "Science"})
@@ -27,3 +28,6 @@ with multiprocessing.Manager() as mgr:
p3.join()
print("Exiting the main process")
if __name__ == '__main__':
main()
+4
View File
@@ -11,6 +11,7 @@ def output(myqueue):
while not myqueue.empty():
print(myqueue.get())
def main():
mylist = [2, 5, 7]
myqueue = Queue()
@@ -24,3 +25,6 @@ p2.join()
print("Queue is empty: ",myqueue.empty())
print("Exiting the main process")
if __name__ == '__main__':
main()
+5
View File
@@ -15,6 +15,8 @@ def myreceiver(r_conn):
break
print("Received message : ", msg)
r_conn.close()
def main():
sender_conn, receiver_conn= Pipe()
p1 = Process(target=mysender, args=(sender_conn, ))
@@ -27,3 +29,6 @@ p1.join()
p2.join()
print("Exiting the main process")
if __name__ == '__main__':
main()
+4
View File
@@ -9,6 +9,7 @@ def printme (lock, msg):
finally:
lock.release()
def main():
with Pool(3) as proc:
lock = Manager().Lock()
func = partial(printme,lock)
@@ -16,3 +17,6 @@ with Pool(3) as proc:
"Grapes","Pears"])
print("Exiting the main process")
if __name__ == '__main__':
main()