Пример #1
0
        def __init__(self, num, stack_size, interrupted):
            """Create the request and reply queues, and 'num' worker threads.
            
            One must specify the stack size of the worker threads. The
            stack size is specified in kilobytes.
            """
            self.requestQueue = queue.Queue(0)
            self.resultsQueue = queue.Queue(0)

            try:
                prev_size = threading.stack_size(stack_size*1024) 
            except AttributeError as e:
                # Only print a warning if the stack size has been
                # explicitly set.
                if not explicit_stack_size is None:
                    msg = "Setting stack size is unsupported by this version of Python:\n    " + \
                        e.args[0]
                    SCons.Warnings.warn(SCons.Warnings.StackSizeWarning, msg)
            except ValueError as e:
                msg = "Setting stack size failed:\n    " + str(e)
                SCons.Warnings.warn(SCons.Warnings.StackSizeWarning, msg)

            # Create worker threads
            self.workers = []
            for _ in range(num):
                worker = Worker(self.requestQueue, self.resultsQueue, interrupted)
                self.workers.append(worker)

            # Once we drop Python 1.5 we can change the following to:
            #if 'prev_size' in locals():
            if 'prev_size' in list(locals().keys()):
                threading.stack_size(prev_size)
Пример #2
0
    def __init__(self, num_threads=4, max_tasks=16, timeout=32,
                 init_local=None, stack_size=None):
        """
        Initialize and start a new thread pool.

        Exactly num_threads will be spawned. At most max_tasks
        can be queued before add() blocks; add() blocks for at
        most timeout seconds before raising an exception.

        You can pass a callable with one argument as init_local
        to initialize thread-local storage for each thread; see
        add() below for how to access thread-local storage from
        your tasks. For example:

            import sqlite3
            ...
            def init_local(local):
                local.connection = sqlite3.connect("some.db")
            ...
            pool = ThreadPool(init_local=init_local)
        """
        assert num_threads > 0
        assert max_tasks > 0
        assert timeout > 0
        # TODO: undocumented and probably a very bad idea
        assert stack_size is None or stack_size > 16*4096
        if stack_size is not None:
            T.stack_size(stack_size)
        self.__queue = Q.Queue(max_tasks)
        self.__timeout = timeout
        for _ in range(num_threads):
            _Worker(self.__queue, init_local)
    def start(self):
        

        # Verify that the user has a reasonable iDigi host set on their device.
        # Start by appending 1 to filename of pushed data
        self.__current_file_number = 1

        # Event to use for notification of meeting the sample threshold
        self.__threshold_event = threading.Event()

        # Count of samples since last data push
        self.__sample_count = 0
        
        #self.fc = f_counter()
        #
        #self.__core.set_service("fc", self.fc)

        # Here we grab the channel publisher
        channels = SettingsBase.get_setting(self, "channels")
        cm = self.__core.get_service("channel_manager")
        cp = cm.channel_publisher_get()

        # And subscribe to receive notification about new samples
    #    if len(channels) > 0:
    #        for channel in channels:
    #            cp.subscribe(channel, self.receive)
    #    else:
    #        cp.subscribe_to_all(self.receive)
        self.lock = threading.Lock()
        print threading.stack_size()
        threading.Thread.start(self)
        self.apply_settings()
        return True
Пример #4
0
    def __init__(self, thread_num = 10):
        # 线程堆栈设置,防止占用大量内存
        stack_size(1024*1024)
        self.threadList = []

        self.threadNum = thread_num
        self.queue = Queue.Queue()
Пример #5
0
def main():

    proxy = getcfg("http_proxy")
    if proxy:
        print "Using http proxy %s" % (proxy,)
        pollers.requestoptions["proxies"] = {"http": proxy, "https": proxy}

    try:
        threading.stack_size(512 * 1024)
    except BaseException as e:
        print "Error changing stack size:", repr(e)

    # Connect to the database...
    Database.get()

    parser = ParserThread()
    parser.start()

    for poller in POLLERS.values():
        poller.start()

    # wait for them to finish!
    for poller in POLLERS.values():
        while poller.is_alive():
            time.sleep(1)

    while parser.is_alive():
        time.sleep(1)

    return
Пример #6
0
def checksingleprocess(ipqueue,cacheResult,max_threads):
    threadlist = []
    threading.stack_size(96 * 1024)
    PRINT('need create max threads count: %d' % (max_threads))
    for i in xrange(1, max_threads + 1):
        ping_thread = Ping(ipqueue,cacheResult)
        ping_thread.setDaemon(True)
        try:
            ping_thread.start()
        except threading.ThreadError as e:
            PRINT('start new thread except: %s,work thread cnt: %d' % (e, Ping.getCount()))
            break
        threadlist.append(ping_thread)
    try:
        quit = False
        while not quit:
            for p in threadlist:
                if p.is_alive():
                    p.join(5)
                elif Ping.getCount() == 0 or cacheResult.queryfinish():
                    quit = True
                    break
    except KeyboardInterrupt:
        PRINT("try to interrupt process")
        ipqueue.queue.clear()
        evt_ipramdomend.set()
    cacheResult.close()
Пример #7
0
 def MakeTest(self, exp: Expression, caseNo: int):
     threading.stack_size(227680000)
     sys.setrecursionlimit(2147483647)
     test = Test()
     test.StartTest(caseNo, 1, 100, 'PEG', lambda w: self.TestPositive(exp, w), lambda w: self.TestNegative(exp, w))
     test.StartTest(caseNo, 101, 1000, 'PEG', lambda w: self.TestPositive(exp, w), lambda w: self.TestNegative(exp, w))
     test.StartTest(caseNo, 1001, 10000, 'PEG', lambda w: self.TestPositive(exp, w), lambda w: self.TestNegative(exp, w))
     test.StartTest(caseNo, 10001, 100000, 'PEG', lambda w: self.TestPositive(exp, w), lambda w: self.TestNegative(exp, w))        
Пример #8
0
def main():
	try:
		print "Start listening..."
		threading.stack_size(1024 * 512)
		server = ThreadingTCPServer(('', 5060), ProxyServer)
		server_thread = threading.Thread(target=server.serve_forever)
		server_thread.start()
	except Exception, msg:
		print "Exception in main: ", msg
Пример #9
0
 def test_various_ops_large_stack(self):
     if verbose:
         print 'with 1MB thread stack size...'
     try:
         threading.stack_size(0x100000)
     except thread.error:
         self.skipTest('platform does not support changing thread stack size')
     self.test_various_ops()
     threading.stack_size(0)
Пример #10
0
 def test_various_ops_small_stack(self):
     if verbose:
         print 'with 256kB thread stack size...'
     try:
         threading.stack_size(262144)
     except thread.error:
         self.skipTest('platform does not support changing thread stack size')
     self.test_various_ops()
     threading.stack_size(0)
Пример #11
0
 def test_various_ops_large_stack(self):
     if verbose:
         print('with 1 MiB thread stack size...')
     try:
         threading.stack_size(0x100000)
     except _thread.error:
         raise unittest.SkipTest(
             'platform does not support changing thread stack size')
     self.test_various_ops()
     threading.stack_size(0)
Пример #12
0
def main():
	try:
		print "Start listening..."
		threading.stack_size(1024 * 512 * 2)
		socket.setdefaulttimeout(30)
		server = ThreadingTCPServer(('', 5070), Socks5Client)
		server_thread = threading.Thread(target=server.serve_forever)
		server_thread.start()
	except Exception, msg:
		print "Exception in main: ", msg
def main():
 #   MiB = 2 ** 20
 #   threading.stack_size(256 * MiB -1)
 #   sys.setrecursionlimit(10 ** 7)
 #   t = threading.Thread(target = main)
    maxvertexes=67108864
    threading.stack_size(maxvertexes)
    sys.setrecursionlimit(maxvertexes)
    thread = threading.Thread(target=main)
    thread.start()
Пример #14
0
def scc_recursive():
    import sys, threading

    sys.setrecursionlimit(3000000)
    threading.stack_size(64 * 1024 * 1024)  # set thread stack 64M
    start = clock()
    thread = threading.Thread(target=SCC("SCC.txt", nodeNum=875714).scc_by_recursive)
    thread.start()
    thread.join()
    end = clock()
    print "run by recursion:", str(end - start)
Пример #15
0
 def test_various_ops_large_stack(self):
     if verbose:
         six.print_('with 1MB thread stack size...')
     try:
         threading.stack_size(0x100000)
     except thread.error:
         if verbose:
             six.print_('platform does not support changing thread stack size')
         return
     self.test_various_ops()
     threading.stack_size(0)
Пример #16
0
 def test_various_ops_small_stack(self):
     if verbose:
         print("with 256kB thread stack size...")
     try:
         threading.stack_size(262144)
     except _thread.error:
         if verbose:
             print("platform does not support changing thread stack size")
         return
     self.test_various_ops()
     threading.stack_size(0)
Пример #17
0
 def test_various_ops_large_stack(self):
     if verbose:
         print("with 1MB thread stack size...")
     try:
         threading.stack_size(0x100000)
     except _thread.error:
         if verbose:
             print("platform does not support changing thread stack size")
         return
     self.test_various_ops()
     threading.stack_size(0)
Пример #18
0
    def run(self, arglist):
        threading.stack_size(65536)
        # Get the sequence variables

        args = self.parseArgsKeyValue(arglist)
        threads = int(args['threads'])
        iterations = int(args.get("interations", 1))

        # Get the list of VMs - this is everything that begins with "clone" (cloned in _TCCloneVMs)
        vms = [x for x in xenrt.TEC().registry.instanceGetAll() if x.name.startswith("clone")]

        self.doVMOperations(vms, threads, iterations)
Пример #19
0
def callWithLargeStack(f,*args):
    import sys
    import threading
    threading.stack_size(2**27)  # 64MB stack
    sys.setrecursionlimit(2**27) # will hit 64MB stack limit first
    # need new thread to get the redefined stack size
    def wrappedFn(resultWrapper): resultWrapper[0] = f(*args)
    resultWrapper = [None]
    #thread = threading.Thread(target=f, args=args)
    thread = threading.Thread(target=wrappedFn, args=[resultWrapper])
    thread.start()
    thread.join()
    return resultWrapper[0]
Пример #20
0
def checksingleprocess(ipqueue,cacheResult,max_threads):
    threadlist = []
    threading.stack_size(96 * 1024)
    evt_finish = threading.Event()
    evt_ready = threading.Event()    
    qsize = ipqueue.qsize()
    maxthreads = qsize if qsize < max_threads else max_threads
    PRINT('need create max threads count: %d,total ip cnt: %d ' % (maxthreads, qsize))
    for i in xrange(1, maxthreads + 1):
        ping_thread = Ping(ipqueue,cacheResult,evt_finish,evt_ready)
        ping_thread.setDaemon(True)
        try:
            ping_thread.start()
        except threading.ThreadError as e:
            PRINT('start new thread except: %s,work thread cnt: %d' % (e, Ping.getCount()))
            "can not create new thread"
            break
        threadlist.append(ping_thread)
    evt_ready.wait()
    error = 0
    try:
        time_begin = time.time()
        logtime = time_begin
        count = Ping.getCount()
        lastcount = count
        while count > 0:
            evt_finish.wait(2)
            time_end = time.time()
            queuesize = ipqueue.qsize()
            count = Ping.getCount()
            if lastcount != count or queuesize > 0:
                time_begin = time_end
                lastcount = count
            else:
                if time_end - time_begin > g_handshaketimeout * 5:
                    dumpstacks()
                    error = 1
                    break
            if time_end - logtime > 60:
                PRINT("has thread count:%d,ip total cnt:%d" % (Ping.getCount(),queuesize))
                logtime = time_end
            count = Ping.getCount()
        evt_finish.set()
    except KeyboardInterrupt:
        PRINT("need wait all thread end...")
        evt_finish.set()
    if error == 0:
        for p in threadlist:
            p.join()
    cacheResult.close()
def create_app(flask_config_name=None, **kwargs):
    """
    Entry point to the Flask RESTful Server application.
    """
    # This is a workaround for Alpine Linux (musl libc) quirk:
    # https://github.com/docker-library/python/issues/211
    import threading
    threading.stack_size(2*1024*1024)

    app = Flask(__name__, **kwargs)

    env_flask_config_name = os.getenv('FLASK_CONFIG')
    if not env_flask_config_name and flask_config_name is None:
        flask_config_name = 'local'
    elif flask_config_name is None:
        flask_config_name = env_flask_config_name
    else:
        if env_flask_config_name:
            assert env_flask_config_name == flask_config_name, (
                "FLASK_CONFIG environment variable (\"%s\") and flask_config_name argument "
                "(\"%s\") are both set and are not the same." % (
                    env_flask_config_name,
                    flask_config_name
                )
            )

    try:
        app.config.from_object(CONFIG_NAME_MAPPER[flask_config_name])
    except ImportError:
        if flask_config_name == 'local':
            app.logger.error(
                "You have to have `local_config.py` or `local_config/__init__.py` in order to use "
                "the default 'local' Flask Config. Alternatively, you may set `FLASK_CONFIG` "
                "environment variable to one of the following options: development, production, "
                "testing."
            )
            sys.exit(1)
        raise

    if app.config['REVERSE_PROXY_SETUP']:
        app.wsgi_app = ProxyFix(app.wsgi_app)

    from . import extensions
    extensions.init_app(app)

    from . import modules
    modules.init_app(app)

    return app
Пример #22
0
 def __init__(self, min_workers = 2, stack_size = 512):
     threading.stack_size(stack_size * 1024)
     self._logger = logging.getLogger('atp')
     self._tasks = queue.Queue()
     self._running_tasks = []
     self._min_workers = min_workers + 1 # for the monitoring thread
     self._workers = 0
     self._avail_workers = 0
     self._countlck = threading.Lock()
     self._task_added = threading.Event()
     self._killev = threading.Event()
     self._all_died = threading.Event()
     self.add_worker(self._min_workers)
     mt = Task(target=self.__volume_monitor, infinite=True)
     self.run(mt)
Пример #23
0
def main():
	try:
#		for i in xrange(len(remoteList) - 1, -1, -1):
#			if not hostalive(remoteList[i][0]):
#				remoteList.remove(remoteList[i])
#		if len(remoteList) == 0:
#			print "No available server, I'm dead now"
#			return
		print "Check remote list done, start listening..."
		threading.stack_size(1024 * 512)
		server = ThreadingTCPServer(('', 5070), ProxyServer)
		server_thread = threading.Thread(target=server.serve_forever)
		server_thread.start()
	except Exception, msg:
		print "Exception in main: " , msg
Пример #24
0
    def run(self):
        threading.stack_size(1024 * 512 * 2)
        socket.setdefaulttimeout(30)
        server = ThreadingTCPServer(('', CLIENTPORT), Socks5Client)
        server_thread = threading.Thread(target=server.serve_forever)
        server_thread.daemon = True
        
        self.cfg.backup()
        self.cfg.modify("127.0.0.1", str(CLIENTPORT))
        print ">> MOSP client start working"
        server_thread.start()

        while self.run:
            time.sleep(1)

        print ">> Client close successfully"
Пример #25
0
def main():
    try:
        userinfo = getpwnam('bbs')
        os.setuid(userinfo[2])
    except:
        Log.error("Failed to find user 'bbs'!")
        sys.exit(1)

    threading.stack_size(1024*1024) # default stack size: 8M. may exhaust virtual address space
    port = 8080
    server = MyServer(('::', port), DataService)
    print 'Starting at port %d...' % port
    try:
        server.serve_forever()
    except:
        pass
Пример #26
0
def forSector(url,D):
    print("downloading "+url)
    html = getHtml(url)
    imgList = getImageList(html)
    global Dir
    Dir = D +title+'/'
    if not os.path.exists(Dir):
        os.makedirs(Dir)
    global queue
    queue = Queue()
    for x in imgList:
        queue.put(x)
    threading.stack_size(32768*16)
    socket.setdefaulttimeout(10)
    s = Spider(title)
    s.start()
Пример #27
0
def main():
    html = getHtml('http://tieba.baidu.com/p/3825522906?da_from=ZGFfbGluZT1EVCZkYV9wYWdlPTImZGFfbG9jYXRlPXAwMDA1JmRhX2xvY19wYXJhbT0xJmRhX3Rhc2s9dGJkYSZkYV9vYmpfaWQ9MTQ2MDAmZGFfb2JqX2dvb2RfaWQ9MjYwOTgmZGFfdGltZT0xNDM0NDY0MjI3JmRhX3JlZl9maWQ9NjA4&da_sign=a73a0f032613ae50e16f626de80f3fff&tieba_from=tieba_da')
    imgList = getImageList(html)
    global queue
    global Dir
    Dir = '/home/Coderec/d/'
    # +title+'/'
    if not os.path.exists(Dir):
        os.mkdir(Dir)
    queue = Queue()
    for x in imgList:
        queue.put(x)
    threading.stack_size(32768*16)
    socket.setdefaulttimeout(10)
    s = Spider('s1')
    s.start()
def main():
    sys.setrecursionlimit(2000000000)
    threading.stack_size(200000000)
    m = range_input("Enter m", 2, 5)
    n = range_input("Enter n", 2, 8)
    import time
    graph = DGraph(m, n)
    start_time = time.time()
    e_circuit = graph.e_circuit
    print(time.time() - start_time)
    if e_circuit is not None:
        print("E circuit found!")
        print(convert_base(n, e_circuit[0], m) + "".join(
            [convert_base(n, e_circuit[x], m)[-1] for x in range(1, len(e_circuit))]))
    else:
        print("E circuit not found!")

    print(e_circuit)
Пример #29
0
    def run(self, arglist):
        threading.stack_size(65536)
        threads = None

        args = self.parseArgsKeyValue(arglist)
        threads = int(args['threads'])
        instances = int(args['instances'])

        # Generate the list of VM names, which host they will run on and where they're clones from
        # The name will be of the format clonex.y:
        #   x = zone the VM will run on
        #   y = index of the VM on the zone

        self.vmSpecs = [("clone-%s-%d" % (self.zones[x % len(self.zones)], x/len(self.zones)), self.zones[x % len(self.zones)]) for x in range(instances)]

        # We'll run this with a limited number of workers (threads).
        # Each worker thread will pull a VM Spec from the list, clone it, then move onto the next one. The threads will complete when all VMs are cloned
        pClone = map(lambda x: xenrt.PTask(self.doClones), range(threads))
        xenrt.pfarm(pClone)
Пример #30
0
def checksingleprocess(ipqueue,cacheResult,max_threads):
    threadlist = []
    threading.stack_size(96 * 1024)
    PRINT('need create max threads count: %d' % (max_threads))
    for i in xrange(1, max_threads + 1):
        ping_thread = Ping(ipqueue,cacheResult)
        ping_thread.setDaemon(True)
        try:
            ping_thread.start()
        except threading.ThreadError as e:
            PRINT('start new thread except: %s,work thread cnt: %d' % (e, Ping.getCount()))
            break
        threadlist.append(ping_thread)
    try:
        for p in threadlist:
            p.join()
    except KeyboardInterrupt:
        evt_ipramdomend.set()
    cacheResult.close()
Пример #31
0
def dfs(v, visited, graph, cnt):
    visited[v] = cnt
    for w in graph[v]:
        if visited[w] == False:  # посещён ли текущий сосед?
            dfs(w, visited, graph, cnt)


def main():
    n, m = map(int, sys.stdin.readline().split())
    graph = [[] for _ in range(n)]
    visited = [False] * n
    while m:
        v, u = map(int, sys.stdin.readline().split())
        graph[v - 1].append(u - 1)
        m -= 1
    cnt = 0
    for v in range(n):
        if visited[v] == False:
            cnt += 1
            dfs(v, visited, graph, cnt)
    print(*visited)


if __name__ == "__main__":
    main()

setrecursionlimit(10**9)
threading.stack_size(2**26)  # лучше использовать именно эту константу
thread = threading.Thread(target=main)
thread.start()
Пример #32
0
def list_ping():
    if g_useOpenSSL == 1:
        PRINT("suport PyOpenSSL")
    threadlist = []
    iprangelist = []
    "split ip,check ip valid and get ip begin to end"
    iplineslist = re.split("\r|\n", ip_str_list)
    for iplines in iplineslist:
        if len(iplines) == 0 or iplines[0] == '#':
            continue
        ips = re.split(",|\|", iplines)
        for line in ips:
            if len(line) == 0 or line[0] == '#':
                continue
            begin, end = splitip(line)
            if checkipvalid(begin) == 0 or checkipvalid(end) == 0:
                PRINT("ip format is error,line:%s, begin: %s,end: %s" %
                      (line, begin, end))
                sys.exit(1)
            nbegin = from_string(begin)
            nend = from_string(end)
            while nbegin <= nend:
                g_queue.put(nbegin)
                nbegin += 1

    threading.stack_size(96 * 1024)
    qsize = g_queue.qsize()
    maxthreads = qsize if qsize < g_maxthreads else g_maxthreads
    PRINT('need create max threads count: %d,total ip cnt: %d ' %
          (maxthreads, qsize))
    for i in xrange(1, maxthreads + 1):
        ping_thread = Ping()
        ping_thread.setDaemon(True)
        try:
            ping_thread.start()
        except threading.ThreadError as e:
            PRINT('start new thread except: %s,work thread cnt: %d' %
                  (e, Ping.ncount))
            "can not create new thread"
            break
        threadlist.append(ping_thread)
    g_ready.set()
    try:
        time_begin = time.time()
        lastcount = Ping.ncount
        while Ping.ncount > 0:
            g_finish.wait(1)
            time_end = time.time()
            if lastcount != Ping.ncount or g_queue.qsize() > 0:
                time_begin = time_end
                lastcount = Ping.ncount
            else:
                if time_end - time_begin > g_commtimeout * 3:
                    dumpstacks()
                    break
                else:
                    time_begin = time_end
        g_finish.set()
    except KeyboardInterrupt:
        g_finish.set()
        #for thread in threadlist:
        #   thread.join()

    ip_list.sort()

    PRINT('try to collect ssl result')
    op = 'wb'
    if sys.version_info[0] == 3:
        op = 'w'
    ff = open(g_ipfile, op)
    ncount = 0
    for ip in ip_list:
        domain = ip[2]
        PRINT("[%s] %d ms,domain: %s" % (ip[1], ip[0], domain))
        if domain is not None:
            ff.write(ip[1])
            ff.write("|")
            ncount += 1
    PRINT("write to file %s ok,count:%d " % (g_ipfile, ncount))
    ff.close()
Пример #33
0
# Application defaults.
NUM_WORKERS = 5000  # Total number test workers
NUM_PROCESSES = multiprocessing.cpu_count(
) + 1  # Number of processes (cores) to utilize
RAMP_UP_SECS = 0  # Ramp-up time in seconds (default instant)
THREAD_SLEEP = 0.1  # Default time to sleep a thread (eg. during polling)
MAX_INTERRUPTS = 5  # Max Ctrl+C / SIGINTs before hard exit
STACK_SIZE = 32768  # Stack size for new threads
REQUESTS_DEBUG = False  # Dump ugly debug output from requests

# Global timer used by each process.
# FIXME: This will be slightly off for each multiprocess, use multiprocessing.Value() to pass it around.
global_timer = time.time()

# Set stack size for new threads.
threading.stack_size(STACK_SIZE)

# Configure request debugging.
if REQUESTS_DEBUG:
    import http.client as http_client
    import logging
    http_client.HTTPConnection.debuglevel = 1
    logging.basicConfig()
    logging.getLogger().setLevel(logging.DEBUG)
    requests_log = logging.getLogger("requests.packages.urllib3")
    requests_log.setLevel(logging.DEBUG)
    requests_log.propagate = True


class Singleton():
    """
Пример #34
0
socket.setdefaulttimeout(_settings.socket_timeout)

_agentVersion = "1.5.7"

_pymongoVersion = pymongo.version

_pymongoHasC = False

try:
    _pymongoHasC = pymongo.has_c()
except:
    pass

# Try and reduce the stack size.
try:
    threading.stack_size(409600)
except:
    pass


class AgentProcess(threading.Thread):
    """ The parent process - monitors agent process and checks for updates etc """
    def __init__(self, loggerObj, agentDir, existingSessionKey):
        """ Construct the object """
        self.logger = loggerObj
        self.agentDir = agentDir
        self.mmsAgent = MmsAgent(_settings, _agentVersion,
                                 platform.python_version(), _pymongoVersion,
                                 _pymongoHasC,
                                 platform.uname()[1], self.logger,
                                 existingSessionKey)
Пример #35
0
def _apply_hotfixes():
    # This is a workaround for Alpine Linux (musl libc) quirk:
    # https://github.com/docker-library/python/issues/211
    import threading

    threading.stack_size(2 * 1024 * 1024)
Пример #36
0
import sys, threading

sys.setrecursionlimit(800000) # Increase recursion limit for large G (>800k vertices)
threading.stack_size(67108864) # Increase stack size to avoid segmentation fault

G = {}
stack = []

# Process data
with open('filename.txt', 'r') as file:
    for line in file:
        l = [int(s) for s in line.split()]

        if l[0] not in G:
            G[l[0]] = set({l[1]})

            if l[1] not in G: # In case v does not have any forward directions
                G[l[1]] = set()

        else:
            G[l[0]].add(l[1])

            if l[1] not in G:
                G[l[1]] = set()

# Depth-first search, 2 passes
def dfs(G, v, step, V = {}, scc_count = 0):
    global stack

    if (step == 'first_step'): # First pass of Kosaraju's algorithm
        if v not in V:
The first line contains two space-separated integers n and m which represent the number of junctions and the roads in the town correspondingly. Then follow m lines, each containing two numbers which describe the roads in the city. Each road is determined by two integers a and b the numbers of junctions it connects.
It is guaranteed that one can get from any junction to any other one along the existing bidirectional roads. Each road connects different junctions, there is no more than one road between each pair of junctions.
If there's no solution, print the single number 0. Otherwise, print m lines each containing two integers p and q — each road's orientation.
'''
#Solution:
'''
Let's picture the problem as a Graph problem. The construction is only possible when all nodes of the graph is a part of a cycle. Only then we will have 2 or more paths to travel to a specific node.
If all nodes are part of a cycle, then there will be a back-edge(in the DFS traversal) for every node leading to any of it's ancestor node, so we can direct the traffic.
To check this we only need to check if there is a Bridge(Cut Edge) present in the graph.
'''
#Code ->
from sys import stdin, stdout, setrecursionlimit
from collections import defaultdict
from threading import stack_size, Thread
setrecursionlimit(10**6)
stack_size(2**25)
adj = defaultdict(list)
visited = [False] * (100001)
intime = [0] * (100001)
outtime = [0] * (100001)
res = []
bridge = False
timer = 0


def dfs(node, par):
    global adj, visited, intime, outtime, res, bridge, timer
    visited[node] = True
    intime[node] = timer
    outtime[node] = timer
    timer += 1
Пример #38
0
import os
import signal
import threading
import time
import xml.dom.minidom as xml

from lofarpipe.support.pipelinelogging import log_process_output
from lofarpipe.support.utilities import spawn_process
from lofarpipe.support.lofarexceptions import PipelineQuit
from lofarpipe.support.jobserver import job_server
import lofarpipe.support.lofaringredient as ingredient
from lofarpipe.support.xmllogging import add_child

# By default, Linux allocates lots more memory than we need(?) for a new stack
# frame. When multiplexing lots of threads, that will cause memory issues.
threading.stack_size(1048576)


class ParamikoWrapper(object):
    """
    Sends an SSH command to a host using paramiko, then emulates a Popen-like
    interface so that we can pass it back to pipeline recipes.
    """
    def __init__(self, paramiko_client, command):
        self.returncode = None
        self.client = paramiko_client
        self.chan = paramiko_client.get_transport().open_session()
        self.chan.get_pty()
        self.chan.exec_command(command)
        self.stdout = self.chan.makefile('rb', -1)
        self.stderr = self.chan.makefile_stderr('rb', -1)
Пример #39
0
    explored[i] = True

    for v in graph[i]:
        if not explored[v]:
            DFS_2(graph, v)

    scc_size += 1


def kosarajuSCCsizes(graph, graph_rev):

    DFS_Loop_1(graph_rev, len(graph))
    res = DFS_Loop_2(graph)

    return res


def main():

    graph, graph_rev = readDirectedGraph('SCC.txt')

    res = kosarajuSCCsizes(graph, graph_rev)

    print(','.join(map(lambda x: str(x), sorted(res)[::-1][:5])))


if __name__ == '__main__':
    threading.stack_size(67108864)  # 64MB stack
    sys.setrecursionlimit(2**20)  # approx 1 million recursions
    thread = threading.Thread(target=main)  # instantiate thread object
    thread.start()  # run program at target
Пример #40
0
    console_log.setLevel(
        logging.INFO)  # Display only important info to console

# Load plugins
from Plugin import PluginManager
PluginManager.plugin_manager.loadPlugins()
config.loadPlugins()
config.parse()  # Parse again to add plugin configuration options

# Log current config
logging.debug("Config: %s" % config)

# Modify stack size on special hardwares
if config.stack_size:
    import threading
    threading.stack_size(config.stack_size)

# Use pure-python implementation of msgpack to save CPU
if config.msgpack_purepython:
    os.environ["MSGPACK_PUREPYTHON"] = "True"

# Socket monkey patch
if config.proxy:
    from util import SocksProxy
    import urllib2
    logging.info("Patching sockets to socks proxy: %s" % config.proxy)
    if config.fileserver_ip == "*":
        config.fileserver_ip = '127.0.0.1'  # Do not accept connections anywhere but localhost
    SocksProxy.monkeyPatch(*config.proxy.split(":"))
elif config.tor == "always":
    from util import SocksProxy
Пример #41
0
from __future__ import print_function
from ortools.constraint_solver import pywrapcp
from ortools.constraint_solver import routing_enums_pb2

import os
import numpy as np
from matplotlib import pyplot as plt
from collections import namedtuple
import csv
import sys
import threading
sys.setrecursionlimit(100000)
threading.stack_size(200000000)

#thread = threading.Thread #(target=your_code)
#thread.start(routing_enums_pb2)

speed = 50
max_dist = 3000  #maximum_distance
time =  3000/50 #max_dist/speed

Dist_matrix = []



i = 0
transposed_row = []
popn = []

#with open('111-152.csv') as csvDataFile:
with open('Route_Distances6.csv') as csvDataFile:
Пример #42
0
from sys import setrecursionlimit
import threading

threading.stack_size(8 * 1024 * 1024 * 16)
setrecursionlimit(10**9)


def main():
    def dfs(vertex):
        nonlocal cycle_start
        graph[vertex][0] = 1
        path.append(vertex + 1)
        for i in range(1, len(graph[vertex])):
            next_vertex = graph[vertex][i]
            if graph[next_vertex][0] == 0:
                if dfs(graph[vertex][i]):
                    stack.append(vertex + 1)
                    return True
            elif graph[next_vertex][0] == 1:
                stack.append(vertex + 1)
                cycle_start = next_vertex + 1
                return True
        graph[vertex][0] = 2
        path.pop()

    with open("cycle.in") as f:
        n, m = list(map(int, f.readline().split()))
        graph = [[0] for _ in range(n)]
        cycle_start = None

        for i in range(m):
Пример #43
0
                track = []
                track = self.DFS(track, i, visited)
                for j in track:
                    answer[j] = len(track)
        return answer


############################################################################


def solve():
    # Read input data
    n, m = readLine()
    g = Graph(n)
    for i in range(m):
        v, u = readLine()
        v -= 1
        u -= 1
        g.addEdge(v, u)
    answer = g.connectedComponents(n)

    for i in range(n):
        print(answer[i], end=" ")


sys.setrecursionlimit(3 * 10**5)
threading.stack_size(8 * 10**7)
t = threading.Thread(target=solve)
t.start()
t.join()
Пример #44
0
__author__ = 'ramon'

import sys
import threading

threading.stack_size(10000000)
sys.setrecursionlimit(10**6)


def get_input(filename):
    graph_map = {}

    for line in open(filename, 'r').readlines():
        values = [int(val) for val in line.split()]
        key1 = values.pop(0)
        key2 = values.pop(0)

        if not key1 in graph_map:
            graph_map[key1] = []

        if not key2 in graph_map:
            graph_map[key2] = []

        graph_map[key1].extend([key2])

    return graph_map


def DFS(graph_map, start):
    global t, finished_time, visited, counter
cli contains exactly 2 variables. For each wire 𝑖, denote by 𝑥𝑖 a binary variable which takes value 1
if the wire is bent to the right and 0 if the wire is bent to the left. For each 𝑖, 𝑥𝑖 must be either 0 or 1.
Also, some pairs of wires intersect in some positions. For example, it could be so that if wire 1 is bent
to the left and wire 2 is bent to the right, then they intersect. We want to write down a formula which
is satisfied only if no wires intersect. In this case, we will add the cli (𝑥1 𝑂𝑅 𝑥2) to the formula
which ensures that either 𝑥1 (the first wire is bent to the right) is true or 𝑥2 (the second wire is bent
to the left) is true, and so the particular crossing when wire 1 is bent to the left AND wire 2 is bent to
the right doesn’t happen whenever the formula is satisfied. We will add such a cli for each pair of
wires and each pair of their positions if they intersect when put in those positions. Of course, if some
pair of wires intersects in any pair of possible positions, we won’t be able to design a circuit. Your task
is to determine whether it is possible, and if yes, determine the direction of bending for each of the
wires
"""

sys.setrecursionlimit(10**6) # max recursion depth as instructed
threading.stack_size(2**26)  # set the stack size for every new thread 

def cc(edges):
    v = set(v for v in itertools.chain(*edges))
    ind = dict((v, -1) for v in v)
    ll = ind.copy()
    ccs = []
    index = 0
    stack = []
    for v in v:
        if ind[v] < 0:
            st(v, edges, ind, ll, ccs, index, stack)
    return ccs

def st(vertex, edges, ind, ll, ccs, index, stack):
    ind[vertex] = index
Пример #46
0
 def save(self):
     print(threading.stack_size())
     threading.stack_size(201326592)  # 64*3 MB
     thread = threading.Thread(daemon=True, target=lambda: self.__save__())
     thread.start()
     thread.join()
Пример #47
0
#!/usr/bin/env python
# coding:utf-8

import atexit
import os
import sys
import time
import platform
import shutil
from datetime import datetime

# reduce resource request for threading
# for OpenWrt
import threading
try:
    threading.stack_size(128 * 1024)
except:
    pass

try:
    import tracemalloc
    tracemalloc.start(10)
except:
    pass

try:
    raw_input  # python 2
except NameError:
    raw_input = input  # python 3

current_path = os.path.dirname(os.path.abspath(__file__))
        counter = 0
        for i in range(2 * N, 0, -1):
            node = order[i]
            if not mark[node]:
                DFS(Grev, node, mark, counter)

            counter += 1
            sys.stdout.write("\rFile %s: %s" % (ext, (float(2 * N - i + 1) /
                                                      (2 * N)) * 100) + "%")
            sys.stdout.flush()

        #4. Check if x and -x have same scc value
        print ""
        idx = 1
        while idx < len(G):
            if scc[idx] == scc[idx + 1]:
                res[ext] = 0
                break
            idx += 2
        print "Done"
        gc.collect()

    print ''.join(map(str, res[1:]))


if __name__ == '__main__':
    sys.setrecursionlimit(2**20)
    threading.stack_size(2**30)
    thread = threading.Thread(target=main)
    thread.start()
#coding: utf8
import threading
import traceback
import random
import time
import os
import socket

import libtorrent as lt

threading.stack_size(200 * 1024)
socket.setdefaulttimeout(30)


def fetch_torrent(session, ih, timeout):
    name = ih.upper()
    url = 'magnet:?xt=urn:btih:%s' % (name, )
    data = ''
    params = {
        'save_path': '/tmp/downloads/',
        'storage_mode': lt.storage_mode_t(2),
        'paused': False,
        'auto_managed': False,
        'duplicate_is_error': True
    }
    try:
        handle = lt.add_magnet_uri(session, url, params)
    except:
        return None
    status = session.status()
    handle.set_sequential_download(1)
#!/usr/bin/python3
import sys
import threading

sys.setrecursionlimit(10**8)  # max depth of recursion
threading.stack_size(2**26)  # new thread will get stack of such size

# Task: You are given a binary tree with integers as its keys. You need to test whether it is a correct binary
#   search tree. The definition of the binary search tree is the following: for any node of the tree, if its
#   key is x, then for any node in its left subtree its key must be strictly less than x, and for any node in
#   its right subtree its key must be strictly greater than x. In other words, smaller elements are to the
#   left, and bigger elements are to the right. You need to check whether the given binary tree structure
#   satisfies this condition. You are guaranteed that the input contains a valid binary tree. That is, it is a
#   tree, and each node has at most two children.


class Node:
    def __init__(self, key, left, right):
        self.key = int(key)
        # the index of the left child
        self.left = int(left)
        # the index of teh right child
        self.right = int(right)


def is_bst(tree):
    """wrapper function"""
    # 如果二元搜尋樹沒有任何一個元素,則回傳True
    if not len(tree):
        return True
    return is_bst_util(tree, 0, -float('inf'), float('inf'))
Пример #51
0
    del app 
    del Dialog
    del ui
    if action  == 'stop':
        with open(databaseFile, 'r') as  json_file:
            database = json.load(json_file)
        data, userExists = getUserData(database, userName)
        if userExists:
            openCreatePage(data, databaseFile, userName, database)
        else : 
            sys.exit(-1)

if __name__ == "__main__":
    #resource.setrlimit(resource.RLIMIT_STACK, (2**29,-1))
    #sys.setrecursionlimit(10**6)
    threading.stack_size(1228800) #multiple of 4kB
    database = ""
    databaseFile = '..' + os.path.sep +'data' + os.path.sep + 'data.json'
    with open(databaseFile, 'r') as  json_file:
        database = json.load(json_file)
    totalPath =  os.path.abspath(os.getcwd()).split(os.path.sep)
    backupPath = totalPath [0] + os.path.sep + totalPath[1] + os.path.sep  + totalPath[2] + os.path.sep
    try :
        with open(backupPath +'SC_backupBefore.json', 'w') as json_write :
            json.dump(database, json_write)
    except : 
        print("Wrong backup path !")
        sys.exit(-1)
    #print(backupPath)
    data, userName = loginUser(database, databaseFile)
    
Пример #52
0
def main():
    pass


if __name__ == '__main__':
    sync_with_stdio(False)

    if 'PyPy' in sys.version:
        from _continuation import continulet

        def bootstrap(c):
            callable, arg = c.switch()
            while True:
                to = continulet(lambda _, f, x: f(x), callable, arg)
                callable, arg = c.switch(to=to)

        c = continulet(bootstrap)
        c.switch()

        main()

    else:
        import threading

        sys.setrecursionlimit(2097152)
        threading.stack_size(134217728)

        main_thread = threading.Thread(target=main)
        main_thread.start()
        main_thread.join()
import collections, sys, threading
sys.setrecursionlimit(10**9)
threading.stack_size(10**8)


def main():
    pass


threading.Thread(target=main).start()
Пример #54
0
            stack.append(i)
            visited[i] = 1
            explore(lst, stack, visited)
    visited[stack[-1]] = 2
    stack.pop()


def main():
    v, e = map(int, sys.stdin.readline().split())
    lst = [set() for i in range(v)]
    for i in range(e):
        a, b = map(int, sys.stdin.readline().split())
        lst[a - 1].add(b - 1)

    visited = [0 for i in range(v)]

    for i in range(v):
        if visited[i] == 0:
            stack = [i]
            visited[i] = 1
            explore(lst, stack, visited)

    sys.stdout.write('NO')


sys.setrecursionlimit(1 << 30)
threading.stack_size(1 << 27)

main_thread = threading.Thread(target=main)
main_thread.start()
main_thread.join()
Пример #55
0
 def __init__(self):
     self.logger = LoggerRouter().getLogger(__name__)
     self.workers = {}
     self.started = False
     stack_size(65536)
Пример #56
0
import sys
import threading

sys.setrecursionlimit(10**6)
threading.stack_size(2**25)


def in_order(index, tree):
    if tree[index][1] != -1:
        in_order(tree[index][1], tree)
    print(tree[index][0], end=' ')
    if tree[index][2] != -1:
        in_order(tree[index][2], tree)


def pre_order(index, tree):
    print(tree[index][0], end=' ')
    if tree[index][1] != -1:
        pre_order(tree[index][1], tree)
    if tree[index][2] != -1:
        pre_order(tree[index][2], tree)


def post_order(index, tree):
    if tree[index][1] != -1:
        post_order(tree[index][1], tree)
    if tree[index][2] != -1:
        post_order(tree[index][2], tree)
    print(tree[index][0], end=' ')

## Kosaraju's Algorithm to find strongly connected components (SCC)
## Author: Jitendra Bhamare

import sys, threading
sys.setrecursionlimit(800000)
threading.stack_size(67108864)


def main():
    ###  Load an input text file and generate graph and reversed graph from it.
    input_file = open("SCC.txt", "r")
    data = input_file.readlines()
    # Declare num of nodes from input files. always define with one extra node
    num_nodes = 875715

    graph = [[] for i in range(num_nodes)]
    rgraph = [[] for i in range(num_nodes)]

    for item in data:
        item = item.split()
        graph[int(item[0])] += [int(item[1])]
        rgraph[int(item[1])] += [int(item[0])]

    ### Intialize lists for pass1
    explored = [False] * num_nodes
    leader = [0] * num_nodes
    #order = [0] * num_nodes
    order = []

    ## DFS_Loop function
    def DFS_Loop(graph, pass_list):
Пример #58
0
import numpy as np
# back propagation requires a copy of the previous neurons
#set number of neurons here
import sys
import threading
import psutil
p = psutil.Process()
p.cpu_affinity([])
threading.stack_size(2 ** 27 - 1)   #(around 17 mb,shoud be enough
sys.setrecursionlimit(7777777)      #change along with stack size and size of neuralnet & bptt/tpbtt depth
#-----------------------------------------------------------------------------------------------------------------------
def reLU(x):
  global reLUout
  if x > 0:
    reLUout = x
  else:
    a = 0.001                                      #zero for normal reLU, small number for leaky reLU,keep it as a learned parameter for Para Relu(effective not efficient,evolution may a good way to implement if there are  other parameterss that would also evolve)
    reLUout = x*a  
  return reLUout  
  
def sigmoid(x):                                   #requires euler's number
  global sigout 
  eulern = 2.71828182845904523536028747135266249775
  var = 0.0072973525693                                     
  sigout = 1 / ( 1 + ( eulern ** ( var * x * (-1) ) ) )
  return sigout

#--------------------------------------------------------------------------------------------------------------------------------------------

def memories():
  global input
Пример #59
0
                    msg = "Setting stack size is unsupported by this version of Python:\n    " + \
                        e.args[0]
                    SCons.Warnings.warn(SCons.Warnings.StackSizeWarning, msg)
            except ValueError, e:
                msg = "Setting stack size failed:\n    " + str(e)
                SCons.Warnings.warn(SCons.Warnings.StackSizeWarning, msg)

            # Create worker threads
            self.workers = []
            for _ in range(num):
                worker = Worker(self.requestQueue, self.resultsQueue,
                                interrupted)
                self.workers.append(worker)

            if 'prev_size' in locals():
                threading.stack_size(prev_size)

        def put(self, task):
            """Put task into request queue."""
            self.requestQueue.put(task)

        def get(self):
            """Remove and return a result tuple from the results queue."""
            return self.resultsQueue.get()

        def preparation_failed(self, task):
            self.resultsQueue.put((task, False))

        def cleanup(self):
            """
            Shuts down the thread pool, giving each worker thread a
Пример #60
0
#!/usr/bin/python
#coding:utf-8

import threading
from threading import Thread, Lock
from Queue import Queue
import time
import random
from threading import stack_size
import datetime
import multiprocessing

import gevent
import gevent.monkey
# gevent.monkey.patch_socket()
stack_size(32768 * 16)


class ThreadTool:
    def __init__(self, isThread=1, needfinishqueue=0, deamon=True):
        self.isThread = isThread
        self.idletask = {}
        self.Threads = []
        self.alivenum = 0
        self.needfinishqueue = needfinishqueue
        self.running = 0
        self.threads_num = 10
        self.deamon = deamon
        self.job = None
        self.default_object = None
        if self.isThread == 1: