Created
November 22, 2021 14:05
-
-
Save ixtiyoruz/00d3cf825ea0981f215a90c86aed63a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
multithreading vs multiprocessing |
Author
ixtiyoruz
commented
Nov 22, 2021
This runs thread inside a process
import cv2
import time
import os
import multiprocessing
import time
from threading import Thread
# run from video
class TestMP(multiprocessing.Process):
def __init__(self, send, recv, data_stream):
multiprocessing.Process.__init__(self)
self.send = send
self.recv = recv
# self.queue = data_stream
self.queue = []
# does not work
# self.locker = multiprocessing.Lock()
def run(self):
self.thread = Thread(target=self.run_thread)
self.thread.start()
while(True):
# counter = self.recv.recv()
if(len(self.queue) > 0):
counter = self.queue.pop(0)
print("counter mp: ", counter)
time.sleep(0.1)
def run_thread(self):
count = 0
while(True):
count +=1
# self.send.send(count)
self.queue.append(count)
print("counter thread: ", count)
time.sleep(0.1)
if __name__ == "__main__":
recv, send = multiprocessing.Pipe(False)
data_stream = multiprocessing.Queue()
testmp = TestMP(send, recv, data_stream)
testmp.start()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment