Last active
January 28, 2016 15:34
-
-
Save avamsi/bc7d18a3c1c4a75757ec to your computer and use it in GitHub Desktop.
This file contains 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
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() |
This file contains 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
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