def test_keyTestExecute(self): self.arg1 = None self.keys = {} sch = threadly.Scheduler(10) sch.execute(self.arguments, args=("test", ), kwargs={"key": "test"}) time.sleep(0.1) self.assertEqual(self.arg1, "test") self.assertEqual(self.keys["key"], "test") sch.shutdown().get()
def test_keyTest2(self): global ADD p = threadly.Scheduler(10) for i in xrange(100): p.schedule(TEST2, key="BLAH") for k in p._Scheduler__keys: while p._Scheduler__keys[k].size() > 0: time.sleep(0.1) self.assertEqual(1267650600228229401496703205376, ADD) p.shutdown().get()
def test_futureDoubleSet(self): global clf, llf sch = threadly.Scheduler(10) LF1 = threadly.ListenableFuture() LF2 = sch.schedule_with_future(callLF, delay=0.100, args=(LF1, )) self.assertTrue(LF1.get()) self.assertTrue(LF2.get()) LF3 = sch.schedule_with_future(callLF, delay=0.100, args=(LF1, )) self.assertFalse(LF3.get()) self.assertEqual(10, sch.get_poolsize()) sch.shutdown().get()
def test_futureCallerExceptions(self): global clf, llf sch = threadly.Scheduler(10) LF1 = threadly.ListenableFuture() LF1.add_listener(listenException) LF1.add_listener(listenException) LF1.add_callable(callException) LF2 = sch.schedule_with_future(callLF, delay=0.100, args=(LF1, )) self.assertTrue(LF1.get()) self.assertTrue(LF2.get()) sch.shutdown().get()
def test_keyTest3(self): global ADD ADD = 1 p = threadly.Scheduler(10) fl = [] for i in xrange(100): f = p.schedule_with_future(TEST2, key="BLAH", delay=0.001 * i) fl.append(f) for f in fl: f.get(100) self.assertEqual(1267650600228229401496703205376, ADD) p.shutdown().get()
def test_shutdownTest1(self): p = threadly.Scheduler(1) p.schedule(time.sleep, recurring=True, args=(0.1, )) p.schedule(time.sleep, recurring=True, args=(0.1, )) p.schedule(time.sleep, recurring=True, args=(0.1, )) p.schedule(time.sleep, recurring=True, args=(0.1, )) p.schedule(time.sleep, recurring=True, args=(0.1, )) p.schedule(time.sleep, recurring=True, args=(0.1, )) p.schedule(time.sleep, recurring=True, args=(0.1, )) p.schedule(time.sleep, recurring=True, args=(0.1, )) p.schedule(time.sleep, recurring=True, args=(0.1, )) time.sleep(0.1) p.shutdown_now()
def test_keyTestSchedule(self): self.arg1 = None self.keys = {} sch = threadly.Scheduler(10) sch.schedule(self.arguments, recurring=True, delay=0.010, args=("test", ), kwargs={"key": "test"}, key="TEST") time.sleep(0.2) self.assertEqual(self.arg1, "test") self.assertEqual(self.keys["key"], "test") sch.shutdown().get()
def test_timeingTest1(self): global LASTRUN global RUNS p = threadly.Scheduler(2) LASTRUN = time.time() p.schedule(TEST, delay=0.01, recurring=True) time.sleep(0.500) p.remove(TEST) p.shutdown_now() AVG = sum(RUNS) / float(len(RUNS)) self.assertTrue(AVG < 0.0130) self.assertTrue(AVG > 0.0099)
def test_keyTest(self): global CALL global PASSED p = threadly.Scheduler(10) LASTRUN = time.time() threads = list() for i in xrange(1001): p.schedule(TEST1, key="TEST") for k in p._Scheduler__keys: while p._Scheduler__keys[k].size() > 0: time.sleep(0.1) p.shutdown_now() self.assertEqual(1, CALL)
def __init__(self, ip, port, hflip=False, vflip=False, delayMS=50): self.__camera = PiCamera(framerate=30) self.__delay = delayMS # self.__camera.resolution= (1920,1080) # self.__camera.resolution= (1280,720) self.__camera.resolution= (853,480) self.__camera.vflip = vflip self.__camera.hflip = hflip self.__camera.exposure_mode = 'auto' self.__pool = threadly.Scheduler(5) self.__SE = litesockets.SocketExecuter(scheduler=self.__pool) self.__server = self.__SE.createTCPServer(ip, port) self.__clients = {} self.__good = [] self.__log = logging.getLogger("MjpegServer:{}:{}".format(ip, port)) self.__server.setOnClient(self.__acceptor) self.__server.start() self.__log.info("New MjpegServer started! "+str(self.__delay))
def test_removeScheduledTest2(self): p = threadly.Scheduler(30) LASTRUN = time.time() p.schedule(time.time, delay=10, recurring=True) p.schedule(TEST, delay=10000, recurring=True) p.schedule(TEST, delay=10000, recurring=True) p.schedule(TEST, delay=10000, recurring=True) p.schedule(TEST, delay=10000, recurring=True) p.remove(TEST) self.assertEqual(4, p._Scheduler__delayed_tasks.size()) p.remove(TEST) self.assertEqual(3, p._Scheduler__delayed_tasks.size()) p.remove(TEST) self.assertEqual(2, p._Scheduler__delayed_tasks.size()) p.remove(TEST) self.assertEqual(1, p._Scheduler__delayed_tasks.size()) p.remove(TEST) self.assertEqual(1, p._Scheduler__delayed_tasks.size()) p.shutdown().get()
def test_futureTest1(self): global clf, llf sch = threadly.Scheduler(1) LF1 = threadly.ListenableFuture() LF2 = sch.schedule_with_future(callLF, delay=0.100, args=(LF1, )) LF2.add_listener(listenFromFuture) LF2.add_callable(callFromFuture, args=(1, )) LF1.add_listener(listenFromFuture) LF1.add_callable(callFromFuture, args=(1, )) self.assertTrue(LF1.get()) self.assertTrue(LF2.get()) # Make sure all callbacks finish with lock: time.sleep(0.1) self.assertEqual(2, llf) self.assertEqual(2, clf) LF2.add_listener(listenFromFuture) LF2.add_callable(callFromFuture, args=(1, )) LF1.add_listener(listenFromFuture) LF1.add_callable(callFromFuture, args=(1, )) self.assertEqual(4, llf) self.assertEqual(4, clf) sch.shutdown().get()
def main(): args = create_parser().parse_args() threads = int(args.threads) EXEC = threadly.Scheduler(threads) server_domain = args.server_url if args.server_url[:8] == "https://": server_domain = args.server_url[8:] elif args.server_url[:7] == "http://": server_domain = args.server_url[7:] if os.environ.get('BBMIGRATE_IN_DOCKER') == "true": open("{}/.gitconfig".format(str(Path.home())), "w").write("[credential]\n helper = store\n") open("{}/.git-credentials".format(str(Path.home())), "w").write("https://{}:{}@{}\nhttps://{}:{}@{}\n".format( args.server_user, urllib.parse.quote(args.server_pass), server_domain, args.cloud_user, urllib.parse.quote(args.cloud_pass), "bitbucket.org")) # setting up directory for repo clones dir_name = os.path.abspath(args.repo_path) clone_dir = dir_name try: if not os.path.isdir(clone_dir): os.mkdir(clone_dir) except OSError: logger.exception(f'Failed to create directory {clone_dir}') sys.exit(1) logger.info("Saving repos to path:{}".format(clone_dir)) # initializing Bitbucket Server instance try: bitbucket_server = Bitbucket(url=args.server_url, username=args.server_user, password=args.server_pass) except Error: logger.exception( f'Unable to instantiate Bitbucker Server connection! One or more parameters may be missing or malformed.' ) sys.exit(1) try: projects_gen = bitbucket_server.project_list() except Error: logger.exception( f'Failed to retrieve data from Bitbucket Server {args.server_url}') sys.exit(1) projects = [] logger.info( f'Retrieving projects and repos from Bitbucket Server {args.server_url}' ) for p in projects_gen: new_project_key = p['key'].replace("-", "") project_dir = os.path.join(clone_dir, new_project_key) p['project_dir'] = project_dir p['new_project_key'] = new_project_key projects.append(p) try: repos = bitbucket_server.repo_list(project_key=p['key']) except Error: logger.exception( "Failed to gather repository list from Bitbucket Server project {}" .format(p['name'])) sys.exit(1) logger.info( "Retrieving repositories from Bitbucket Server project {}".format( p['name'])) p['repos'] = [] for r in repos: new_repo_name = new_project_key.lower() + "." + r['slug'].lower() repo_dir = os.path.join(project_dir, r['slug']) new_repo_git = "https://bitbucket.org/" + args.workspace + "/" + new_repo_name + ".git" old_repo_git = "" for href in r['links']['clone']: if href['href'][:8] == "https://": old_repo_git = href['href'] break r['old_repo_git'] = old_repo_git r['new_repo_git'] = new_repo_git r['new_repo_name'] = new_repo_name r['repo_dir'] = repo_dir p['repos'].append(r) p['repos'] = sorted(p['repos'], key=lambda k: k['slug']) projects = sorted(projects, key=lambda k: k['key']) processing = [] logger.info("Duplicating projects on BBCloud") for p in projects: proj = p f = EXEC.schedule_with_future(bbcloud.duplicate_project, args=(args.cloud_user, args.cloud_pass, args.workspace, proj)) processing.append(f) for lf in processing: lf.get() processing = [] logger.info("Duplicating repos and cloning them on BBCloud") for project in projects: project_name = project['name'] project_dir = project['project_dir'] try: os.mkdir(project_dir) except: pass for repo in project['repos']: p = project r = repo f = EXEC.schedule_with_future( git.backupRepo, args=(args.cloud_user, args.cloud_pass, args.workspace, p, r, clone_dir, args.no_remove_repos)) processing.append(f) if len(processing) > 10: for lf in processing: lf.get() processing = [] for lf in processing: lf.get() # When it's all over, remove the tmp dir to keep things tidy if not args.no_remove_repos: try: os_system_string = "rm -rf " + clone_dir os.system(os_system_string) except OSError: logger.exception(f'Failed to remove temp directory {clone_dir}') sys.exit(1)
import threadly, time, logging, sys, traceback, cProfile, pstats from io import StringIO logging.basicConfig( level=logging.DEBUG, format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s", datefmt="%m-%d %H:%M") sch = threadly.Scheduler(100) llf = list() def pstack(): code = [] for threadId, stack in sys._current_frames().items(): code.append("\n# ThreadID: %s" % threadId) for filename, lineno, name, line in traceback.extract_stack(stack): code.append('File: "%s", line %d, in %s' % (filename, lineno, name)) if line: code.append(" %s" % (line.strip())) for line in code: print(line) def blah(): print("1") pass
def test_exptTest1(self): p = threadly.Scheduler(1) p.execute(EXP) time.sleep(0.1)