Skip to content Skip to sidebar Skip to footer

Is There Something Like Nsoperationqueue From Objectivec In Python?

I'm looking into concurrency options for Python. Since I'm an iOS/macOS developer, I'd find it very useful if there was something like NSOperationQueue in python. Basically, it's a

Solution 1:

have you looked celery as an option? This is what celery website quotes

Celery is an asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operation, but supports scheduling as well.

Solution 2:

I'm looking for it, too. But since it doesn't seem to exist yet, I have written my own implementation:

import time
import threading
import queue
import weakref

classOperationQueue:
    def__init__(self):
        self.thread = None
        self.queue = queue.Queue()

    defrun(self):
        while self.queue.qsize() > 0:
            msg = self.queue.get()
            print(msg)
            # emulate if it cost time
            time.sleep(2)

    defaddOperation(self, string):
        # put to queue first for thread safe.
        self.queue.put(string)
        ifnot (self.thread and self.thread.is_alive()):
            print('renew a thread')
            self.thread = threading.Thread(target=self.run)
            self.thread.start()





myQueue = OperationQueue()
myQueue.addOperation("test1")
# test if it auto free
item = weakref.ref(myQueue)
time.sleep(1)
myQueue.addOperation("test2")
myQueue = None
time.sleep(3)
print(f'item = {item}')
print("Done.")

Post a Comment for "Is There Something Like Nsoperationqueue From Objectivec In Python?"