Latest 100 public
snipts » thread
showing 1-3 of 3 snipts for thread
-
∞ Run Linux command for a designated time
#!/bin/bash # thread - Copyright (C) 2009 Radek Suski <info@sigsiu.net> # license see http://www.gnu.org/licenses/lgpl-2.1.html GNU/LGPL. # You can use, redistribute this file and/or modify it under the terms of the GNU Lesser General Public License version 2.1 function usage { echo echo "Starting command with time limit" echo " Usage: thread [COMMAND]... [SECONDS]...[OPTIONS]" echo echo "If the command to execute consist of more then 1 word, embbed it within quotes" echo "Example: " echo " thread "sleep 25" 5 " echo "will start the sleep process for 25 seconds and kill it after 5 seconds" echo echo "Options" echo " -q quiet no countdown output" } function Sleep { clear if [ $1 -gt 39 ]; then steps=39 step=$[$1/40] else steps=$[$1 + 1] step=1 fi for (( c=1; c<=$1; c++ )) do i=$( ps --ppid $$ | grep $3 ) if ! [ "$i" ]; then echo echo echo "$4 $3 has been already terminated - exiting" echo echo exit 1 fi if [ "$2" != 1 ]; then echo -ne "\r [" Done=$[$1-$c] Done=$[$Done/$step] progress=$[$steps-$Done] for (( i=1; i<$progress; i++ )) do echo -n "=" done todo=$[$steps-$progress] if [ $todo -gt $steps ] || [ $todo -lt 0 ]; then todo=$steps fi for (( i=1; i<=$todo; i++ )) do echo -n "." done s=$[$1-$c] p=$[$c*100] p=$[$p/$1] echo -n "] $p % done. $s seconds to forced termination of "$4 $3". " fi sleep 1 done } for p in "$@" do if [ "$p" == "-q" ]; then quiet=1 fi done case $2 in *[!0-9]* ) echo "Time has to be a numeric value. "$2" is not all numeric" usage exit 1; ;; esac if [ "$1" ] && [ "$2" ] && [ "$1" != "--help" ] && [ "$1" != "--usage" ]; then clear # start the process exec $1 & &>/dev/null clear # get program name run=`echo $1 | awk -F " " '{ print $1 }'` # get proces id pid=`ps --ppid $$ | grep "$run" | awk -F " " '{ print $1 }'` if [ "$quiet" != 1 ]; then echo "Started "$1" for $2 seconds. PID is $pid" echo fi # go sleep Sleep "$2" "$quiet" "$pid" "$run" # after sleep if the process still running i=$( ps --ppid $$ | grep $pid ) if [ "$i" ]; then # display message if [ "$quiet" != 1 ]; then echo echo echo "Process $pid is still running. Killing now" fi # try to kill it while [ "$i" ] do kill -9 $pid i=$( ps --ppid $$ | grep $pid ) # if killed, output messgae if ! [ "$i" ]; then # display message echo "Process $pid has been killed after $2 seconds" echo echo # exit exit 1 fi i=$( ps --ppid $$ | grep $pid ) done fi else echo Unsupported request usage fi
-
∞ Anonymous thread with init
new Thread() { private String someVar; private int someOtherVar; public void run() { // threadcode } public void init(String someVar, int someOtherVar) { this.someVar = someVar; this.someOtherVar = someOtherVar; // other initcode start(); } }.init("", 0);
-
∞ Teste urllib e filtragem de html com e sem Thread
import re import urllib2 import time from threading import Thread def getImageByName(user,links): sock = urllib2.urlopen("http://twitter.com/%s" % user) regex = re.compile(r'id="profile-image"\ssrc="([^"]+)"') link = '' while 1: line = sock.readline() if 'id="profile-image"' in line: link = regex.findall(line)[0] break links.append(link) if __name__ == '__main__': l = ['fabiocerqueira','samelafranca','andreacerqueira','aristenio','pedromenezes','ordepi','ninogiovanny','jack','google','willianmax'] #Fazendo download com uso de thread links = [] th = [] print 'Com thread...' ta = time.time() for i in l: t = Thread(target = getImageByName,args = (i,links),name = i) th.append(t) t.start() while filter(None,map(Thread.isAlive,th)): pass for link in links: print link print time.time() - ta #Fazendo download sem thread. print '-' * 20 print 'Sem thread...' links = [] ta = time.time() for i in l: getImageByName(i,links) for i in links: print i print time.time() - ta


