Skip to content

Instantly share code, notes, and snippets.

@avamsi
Last active January 28, 2016 15:34
Show Gist options
  • Save avamsi/bc7d18a3c1c4a75757ec to your computer and use it in GitHub Desktop.
Save avamsi/bc7d18a3c1c4a75757ec to your computer and use it in GitHub Desktop.
import subprocess
import threading
import time
import Queue
import setq
def get_conf():
while True:
time.sleep(1)
conf_q.update(conf_file.read().split())
def get_link():
while True:
link_q.put(raw_input())
def aria(conf, link):
subprocess.Popen(
r'C:\ConEmu\ConEmu64.exe'
r' -dir E:\new\aria'
' -cmd cmd /c echo ^> conf-{c} ^| link-{l}'
' & aria2c --conf-path=conf/{c} "{l}"'
' & echo {c} >> confs & pause'.format(c=conf, l=link)
)
def main():
while True:
conf, link = conf_q.pop(), link_q.get()
threading.Thread(target=aria, args=(conf, link)).start()
if __name__ == '__main__':
conf_file = open('confs')
conf_q = setq.SetQ(conf_file.read().split())
link_q = Queue.Queue()
threading.Thread(target=get_conf).start()
threading.Thread(target=get_link).start()
main()
import threading as _threading
class SetQ(set):
"""Set with thread safe add, update, pop methods."""
def __init__(self, iterable=[]):
set.__init__(self, iterable)
self.not_empty = _threading.Condition(_threading.Lock())
def add(self, item):
with self.not_empty:
if item not in self:
set.add(self, item)
self.not_empty.notify()
def update(self, iterable):
for item in iterable:
self.add(item)
def pop(self):
with self.not_empty:
while not self:
self.not_empty.wait()
item = set.pop(self)
return item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment