当前位置:首页 > ZBLOG > 正文

zblog多线程使用的简单介绍

今天给各位分享zblog多线程使用的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

python 多进程和多线程配合

由于python的多线程中存在PIL锁,因此python的多线程不能利用多核,那么,由于现在的计算机是多核的,就不能充分利用计算机的多核资源。但是python中的多进程是可以跑在不同的cpu上的。因此,尝试了多进程+多线程的方式,来做一个任务。比如:从中科大的镜像源中下载多个rpm包。

#!/usr/bin/pythonimport reimport commandsimport timeimport multiprocessingimport threadingdef download_image(url):

print '*****the %s rpm begin to download *******' % url

commands.getoutput('wget %s' % url)def get_rpm_url_list(url):

commands.getoutput('wget %s' % url)

rpm_info_str = open('index.html').read()

regu_mate = '(?=a href=")(.*?)(?=")'

rpm_list = re.findall(regu_mate, rpm_info_str)

rpm_url_list = [url + rpm_name for rpm_name in rpm_list] print 'the count of rpm list is: ', len(rpm_url_list) return rpm_url_list123456789101112131415161718192021

def multi_thread(rpm_url_list):

threads = [] # url = ''

# rpm_url_list = get_rpm_url_list(url)

for index in range(len(rpm_url_list)): print 'rpm_url is:', rpm_url_list[index]

one_thread = threading.Thread(target=download_image, args=(rpm_url_list[index],))

threads.append(one_thread)

thread_num = 5 # set threading pool, you have put 4 threads in it

while 1:

count = min(thread_num, len(threads)) print '**********count*********', count ###25,25,...6707%25

res = [] for index in range(count):

x = threads.pop()

res.append(x) for thread_index in res:

thread_index.start() for j in res:

j.join() if not threads: break1234567891011121314151617181920212223242526

def multi_process(rpm_url_list):

# process num at the same time is 4

process = []

rpm_url_group_0 = []

rpm_url_group_1 = []

rpm_url_group_2 = []

rpm_url_group_3 = [] for index in range(len(rpm_url_list)): if index % 4 == 0:

rpm_url_group_0.append(rpm_url_list[index]) elif index % 4 == 1:

rpm_url_group_1.append(rpm_url_list[index]) elif index % 4 == 2:

rpm_url_group_2.append(rpm_url_list[index]) elif index % 4 == 3:

rpm_url_group_3.append(rpm_url_list[index])

rpm_url_groups = [rpm_url_group_0, rpm_url_group_1, rpm_url_group_2, rpm_url_group_3] for each_rpm_group in rpm_url_groups:

each_process = multiprocessing.Process(target = multi_thread, args = (each_rpm_group,))

process.append(each_process) for one_process in process:

one_process.start() for one_process in process:

one_process.join()# for each_url in rpm_url_list:# print '*****the %s rpm begin to download *******' %each_url## commands.getoutput('wget %s' %each_url)123456789101112131415161718192021222324252627282930313233

def main():

url = ''

url_paas = ''

url_paas2 =''

start_time = time.time()

rpm_list = get_rpm_url_list(url_paas) print multi_process(rpm_list) # print multi_thread(rpm_list)

#print multi_process()

# print multi_thread(rpm_list)

# for index in range(len(rpm_list)):

# print 'rpm_url is:', rpm_list[index]

end_time = time.time() print 'the download time is:', end_time - start_timeprint main()123456789101112131415161718

代码的功能主要是这样的:

main()方法中调用get_rpm_url_list(base_url)方法,获取要下载的每个rpm包的具体的url地址。其中base_url即中科大基础的镜像源的地址,比如:,这个地址下有几十个rpm包,get_rpm_url_list方法将每个rpm包的url地址拼出来并返回。

multi_process(rpm_url_list)启动多进程方法,在该方法中,会调用多线程方法。该方法启动4个多进程,将上面方法得到的rpm包的url地址进行分组,分成4组,然后每一个组中的rpm包再最后由不同的线程去执行。从而达到了多进程+多线程的配合使用。

代码还有需要改进的地方,比如多进程启动的进程个数和rpm包的url地址分组是硬编码,这个还需要改进,毕竟,不同的机器,适合同时启动的进程个数是不同的。

2. 利用多线程设计一个程序,一个线程输出26个小写字母,另一个线程输出26个大写字母

public class RunThread implements Runnable{

String name;

Thread runner;

static boolean bool=true;

public RunThread(String threadName) {

name=threadName;

}

public void onStart()

{

runner = new Thread(this);

runner.setName(name);

runner.start();

}

public void run() {

String name=Thread.currentThread().getName();

System.out.println(name + " 线程运行开始!");

while(true){

int index=0;

if(name.equals("小写字母"))

index='a';

else if(name.equals("大写字母"))

index='A';

while(!bool);

bool=false;

for(int i=index;i26+index;i++)

System.out.print((char)i+" ");

System.out.println();

bool=true;

try {

Thread.sleep(3000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

//System.out.println(name + " 线程运行结束!");

}

public static void main(String[] args) {

RunThread a=new RunThread("小写字母");

RunThread b=new RunThread("大写字母");

a.onStart();

b.onStart();

//System.out.println(" 线程运行开始!");

}

}

建立网站使用的程序框架zblog 怎么使用啊?

用ftp把zblog程序传到虚拟主机根目录,windows空间一般是在woot目录下。

也可以把压缩包传到空间,然后在空间控制面板解压到根目录。

直接在浏览器打开绑定好的域名,就可以进入安装向导页面。

设置好管理员名称和密码,点击确定。

用户名,密码是登入后台使用的,一定要记牢。

点击登入后台就可以,配置你的网站了。

可以安装主题,插件,设置分类,发布文章。

zblog多线程使用的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、zblog多线程使用的信息别忘了在本站进行查找喔。

取消
扫码支持 支付码