This repository has been archived on 2024-12-18. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-12-18/src/network/uploadqueue.py
Peter Surda d635e515b9
Big Asyncore update
- most of the stuff is done so it partially works
- disabled pollers other than select (debugging necessary)
- can switch in the settings, section network, option asyncore (defaults
to False)
2017-05-24 16:51:49 +02:00

71 lines
1.8 KiB
Python

from collections import namedtuple
import Queue
import random
from threading import current_thread, enumerate as threadingEnumerate, RLock
import time
#from helper_sql import *
from singleton import Singleton
UploadElem = namedtuple("UploadElem", "stream identifier")
class UploadQueueDeadlineException(Exception):
pass
class UploadQueue(object):
queueCount = 10
def __init__(self):
self.queue = []
self.lastGet = 0
self.getIterator = 0
for i in range(UploadQueue.queueCount):
self.queue.append([])
def put(self, item):
self.queue[random.randrange(0, UploadQueue.queueCount)].append(item)
def get(self):
i = UploadQueue.queueCount
retval = []
while self.lastGet < time.time() - 1 and i > 0:
if len(self.queue) > 0:
retval.extend(self.queue[self.getIterator])
self.queue[self.getIterator] = []
self.lastGet += 1
# only process each queue once
i -= 1
self.getIterator = (self.getIterator + 1) % UploadQueue.queueCount
if self.lastGet < time.time() - 1:
self.lastGet = time.time()
return retval
def streamElems(self, stream):
retval = {}
for q in self.queue:
for elem in q:
if elem.stream == stream:
retval[elem.identifier] = True
return retval
def len(self):
retval = 0
for i in range(UploadQueue.queueCount):
retval += len(self.queue[i])
return retval
def stop(self):
for i in range(UploadQueue.queueCount):
self.queue[i] = []
@Singleton
class AddrUploadQueue(UploadQueue):
pass
@Singleton
class ObjUploadQueue(UploadQueue):
pass