进程:process 是计算机进行资源分配的和调度的基本单位,进程是程序的一次执行活动。容许多个程序加载到内存中实现并发。进程就是为了实现CPU上执行多道程序提出的。python
线程:thread 是计算机可以进行运行调度的最小单位。是进程中的实际运行单位,进程中的每个线程可并行的执行不一样的任务。一个进程包括至少一个线程。编程
协程:coroutine 是一种用户态的轻量级线程,又称为微线程。能够简单理解为单线程,可是拥有本身的寄存器上下文和栈,在不一样的任务上切换。服务器
协程的好处:并发
"原子操做(atomic operation)是不须要synchronized",所谓原子操做是指不会被线程调度机制打断的操做;这种操做一旦开始,就一直运行到结束,中间不会有任何 context switch (切换到另外一个线程)。原子操做能够是一个步骤,也能够是多个操做步骤,可是其顺序是不能够被打乱,或者切割掉只执行部分。视做总体是原子性的核心。less
缺点:异步
GIL:global interpreter lock 保证了同一个CPU同时只能有一条线程被执行。加全局锁的主要缘由在于多年前的处理并发线程的过程当中极可能出现:与线程并发的内存’清理线程‘会错将其余线程的内存清理。为了不这一现象因而粗糙的加上了全局锁。可是在处理IO密集型操做时实际上是没有影响的。socket
实现协程的一个第三方库,能够轻松经过gevent实现并发同步或异步编程,在gevent中用到的主要模式是Greenlet, 它是以C扩展模块形式接入Python的轻量级协程。 Greenlet所有运行在主程序操做系统进程的内部,但它们被协做式地调度。异步编程
import gevent def func1(): print('\033[31;1m函数1 第一步\033[0m') gevent.sleep(2) print('\033[31;1m函数1 第二步\033[0m') def func2(): print('\033[32;1m函数2 第一步\033[0m') gevent.sleep(1) print('\033[32;1m函数2 第二步\033[0m') gevent.joinall([ gevent.spawn(func1), gevent.spawn(func2), ])
结果:当遇到IO操做时自动切换。函数
函数1 第一步
函数2 第一步
函数2 第二步
函数1 第二步高并发
就算是进程之间的Queue可以实现进程之间的通讯,实际上是管道。可是也不能实现多个服务器之间不一样语言之间的通讯。正所以才是rabbitmq的功能体现之处。
send端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#!/usr/bin/env python
import
pika
connection
=
pika.BlockingConnection(pika.ConnectionParameters(
'localhost'
))
channel
=
connection.channel()
#声明queue
channel.queue_declare(queue
=
'hello'
)
#n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange
=
'',
routing_key
=
'hello'
,
body
=
'Hello World!'
)
print
(
" [x] Sent 'Hello World!'"
)
connection.close()
|
receive端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#_*_coding:utf-8_*_
__author__
=
'Alex Li'
import
pika
connection
=
pika.BlockingConnection(pika.ConnectionParameters(
'localhost'
))
channel
=
connection.channel()
#You may ask why we declare the queue again ‒ we have already declared it in our previous code.
# We could avoid that if we were sure that the queue already exists. For example if send.py program
#was run before. But we're not yet sure which program to run first. In such cases it's a good
# practice to repeat declaring the queue in both programs.
channel.queue_declare(queue
=
'hello'
)
def
callback(ch, method, properties, body):
print
(
" [x] Received %r"
%
body)
channel.basic_consume(callback,
queue
=
'hello'
,
no_ack
=
True
)
print
(
' [*] Waiting for messages. To exit press CTRL+C'
)
channel.start_consuming()
|
When RabbitMQ quits or crashes it will forget the queues and messages unless you tell it not to. Two things are required to make sure that messages aren't lost: we need to mark both the queue and messages as durable.
当rabbit退出或者崩溃的时候队列和消息会被丢弃除非你设置一下。为了确保消息不会丢失,咱们须要标记队列和消息都是可持久化的。
一是声明此队列的时候在其后还有一个参数为durable=True ,另个一个为:
channel.basic_publish(exchange='', routing_key="task_queue", body=message, properties=pika.BasicProperties( delivery_mode = 2, ))
阻塞与非阻塞的关键点是:读操做是否等待,而拷贝数据到用户内存即写操做都是会阻塞住的。
而异步指的是,只会发出命令后将其余的操做交给其余线程去作。整个过程永不会阻塞。
IO多路复用的表明select,poll,epoll是假的异步,属于同步。不过是经过epoll负责轮询全部的socket。