python进程&线程
2015-11-05 10:18
621 查看
# -*- coding:UTF-8 -*-
'''
Created on 2015年10月25日
@author: young
'''
import os
import threading
import time
print '多进程创建'
print 'Process (%s) start...' % os.getpid()
s=1111;
pid = os.fork()
if pid==0:
s=222
print 'I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid())
else:
s=333
print 'I (%s) just created a child process (%s).' % (os.getpid(), pid)
print s
print '创建跨平台的多进程'
from multiprocessing import Process
import os
#子进程要执行的代码'
def run_proc(name):
print 'Run child process %s (%s)...' % (name, os.getpid())
print 'Parent process %s.' % os.getpid()
p = Process(target=run_proc, args=('test',))
print 'Process will start.'
p.start()
p.join()
print 'Process end.'
print '###########多线程#################'
def task():
print "%s is running "% (threading.current_thread().name,)
for i in range(5):
print "%s --> %d"%(threading.current_thread().name,i)
time.sleep(1)#暂停1秒
print "%s end..." % (threading.current_thread().name,)
print "%s is running "% (threading.current_thread().name,)
th=threading.Thread(target=task,name="new thread")
th.start()
th.join()#加入到主线程中,等子线程结束后再结束主线程
print "%s end..." % (threading.current_thread().name,)
print '###################'
'''
Created on 2015年10月25日
@author: young
'''
import os
import threading
import time
print '多进程创建'
print 'Process (%s) start...' % os.getpid()
s=1111;
pid = os.fork()
if pid==0:
s=222
print 'I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid())
else:
s=333
print 'I (%s) just created a child process (%s).' % (os.getpid(), pid)
print s
print '创建跨平台的多进程'
from multiprocessing import Process
import os
#子进程要执行的代码'
def run_proc(name):
print 'Run child process %s (%s)...' % (name, os.getpid())
print 'Parent process %s.' % os.getpid()
p = Process(target=run_proc, args=('test',))
print 'Process will start.'
p.start()
p.join()
print 'Process end.'
print '###########多线程#################'
def task():
print "%s is running "% (threading.current_thread().name,)
for i in range(5):
print "%s --> %d"%(threading.current_thread().name,i)
time.sleep(1)#暂停1秒
print "%s end..." % (threading.current_thread().name,)
print "%s is running "% (threading.current_thread().name,)
th=threading.Thread(target=task,name="new thread")
th.start()
th.join()#加入到主线程中,等子线程结束后再结束主线程
print "%s end..." % (threading.current_thread().name,)
print '###################'
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法