Python의 네트워크 프로그래밍 비저항 비동기 협정

##             

:

: IO , ( ), CPU , IO

: IO IO CPU , CPU

, :

:

: , , , ,

: ,

```
# from concurrent.futures import ProcessPoolExecutor
# import os
# import time
# import random
#
# def task():
# print(f'{os.getpid()} is running')
# time.sleep(random.randint(0,2))
# return f'{os.getpid()} is finish'
#
# if __name__ == '__main__':
#
# p = ProcessPoolExecutor(4)
# obj_l1 = []
# for i in range(10):
# obj = p.submit(task,) # .
# obj_l1.append(obj)
#
# # time.sleep(3)
# p.shutdown(wait=True)
# # 1. ,
# # 2. wait = True 10, -1, . .
# # print(666)
# for i in obj_l1:
# print(i.result())
# : .



# : 10 , , .
# ,......

# : 10 4 , . .




# :

# from concurrent.futures import ProcessPoolExecutor
# import os
# import time
# import random
#
# def task():
# print(f'{os.getpid()} is running')
# time.sleep(1)
# return f'{os.getpid()} is finish'
#
# if __name__ == '__main__':
#
# p = ProcessPoolExecutor(4)
#
# for i in range(10):
# obj = p.submit(task,) # .
# print(obj.result())
```

## +

```
# # .
# # 1 requests
# # : (IO ).
# # : , ,( IO,IO )
# import requests
# from concurrent.futures import ProcessPoolExecutor
# from multiprocessing import Process
# import time
# import random
# import os
#
# def get(url):
# response = requests.get(url)
# print(f'{os.getpid()} :{url}')
# time.sleep(random.randint(1,3))
# if response.status_code == 200:
# return response.text
#
#
# def parse(text):
# '''
#
# len .
# :param text:
# :return:
# '''
# print(f'{os.getpid()} :{len(text)}')
#
# # get('http://www.taobao.com')
# # get('http://www.baidu.com')
# # get('http://www.JD.com')
#
# if __name__ == '__main__':
#
# url_list = [
# 'http://www.taobao.com',
# 'http://www.JD.com',
# 'http://www.JD.com',
# 'http://www.JD.com',
# 'http://www.baidu.com',
# 'https://www.cnblogs.com/jin-xin/articles/11232151.html',
# 'https://www.cnblogs.com/jin-xin/articles/10078845.html',
# 'http://www.sina.com.cn',
# 'https://www.sohu.com',
# 'https://www.youku.com',
# ]
# pool = ProcessPoolExecutor(4)
# obj_list = []
# for url in url_list:
# obj = pool.submit(get, url)
# obj_list.append(obj)
#
# pool.shutdown(wait=True)
#
# for obj in obj_list:
# parse(obj.result())
'''

obj_list[0].result()
obj_list[1].result()
obj_list[2].result()
obj_list[3].result()
obj_list[4].result()
'''

# ?
# 1. , .
# 2. , , .
# 1 :
# , , .

'''
2s, 10 :2. s.
: 1s. 10s. 12. .

:
10 , 4 ( ) 4 , ,
, 10 .
10 , .

2s, : 1s, 3s, 3. ( ).
. 10s.
:
10 + , 4 ( ) 4 + ,
, + , 10 + .




'''

# :
# : : , , .

# import requests
# from concurrent.futures import ProcessPoolExecutor
# from multiprocessing import Process
# import time
# import random
# import os
#
# def get(url):
# response = requests.get(url)
# print(f'{os.getpid()} :{url}')
# time.sleep(random.randint(1,3))
# if response.status_code == 200:
# parse(response.text)
#
#
# def parse(text):
# '''
#
# len .
# :param text:
# :return:
# '''
# print(f'{os.getpid()} :{len(text)}')
#
# if __name__ == '__main__':
#
# url_list = [
# 'http://www.taobao.com',
# 'http://www.JD.com',
# 'http://www.JD.com',
# 'http://www.JD.com',
# 'http://www.baidu.com',
# 'https://www.cnblogs.com/jin-xin/articles/11232151.html',
# 'https://www.cnblogs.com/jin-xin/articles/10078845.html',
# 'http://www.sina.com.cn',
# 'https://www.sohu.com',
# 'https://www.youku.com',
# ]
# pool = ProcessPoolExecutor(4)
# for url in url_list:
# obj = pool.submit(get, url)
#
# # pool.shutdown(wait=True)
# print(' ')


# : , . , .
#

# import requests
# from concurrent.futures import ProcessPoolExecutor
# from multiprocessing import Process
# import time
# import random
# import os
#
# def get(url):
# response = requests.get(url)
# print(f'{os.getpid()} :{url}')
# # time.sleep(random.randint(1,3))
# if response.status_code == 200:
# return response.text
#
#
# def parse(obj):
# '''
#
# len .
# :param text:
# :return:
# '''
# time.sleep(1)
# print(f'{os.getpid()} :{len(obj.result())}')
#
# if __name__ == '__main__':
#
# url_list = [
# 'http://www.taobao.com',
# 'http://www.JD.com',
# 'http://www.JD.com',
# 'http://www.JD.com',
# 'http://www.baidu.com',
# 'https://www.cnblogs.com/jin-xin/articles/11232151.html',
# 'https://www.cnblogs.com/jin-xin/articles/10078845.html',
# 'http://www.sina.com.cn',
# 'https://www.sohu.com',
# 'https://www.youku.com',
# ]
# start_time = time.time()
# pool = ProcessPoolExecutor(4)
# for url in url_list:
# obj = pool.submit(get, url)
# obj.add_done_callback(parse) #
# # , , add_done_callback,
# #
# # .
# pool.shutdown(wait=True)
#
# print(f' : {time.time() - start_time}')

# , . : .
# : . .

# : IO , , .

# , IO,
# + . , .


# ! !! , .

# , IO .
# 1. IO . +
# 2. IO << IO +
# 3. IO >= IO , .
```

##

```
# 1 FIFO queue

# import queue
#
# q = queue.Queue(3)
# q.put(1)
# q.put(2)
# q.put(' ')
# # q.put(666)
#
# print(q.get())
# print(q.get())
# print(q.get())


# LIFO .
# import queue
#
# q = queue.LifoQueue()
# q.put(1)
# q.put(3)
# q.put('barry')
#
# print(q.get())
# print(q.get())
# print(q.get())
# print(q.get())


#
# ,(int, ) int , , .
# import queue
# q = queue.PriorityQueue(3)
#
# q.put((10, ' '))
# q.put((-9, ' '))
# q.put((3, ' '))
#
# print(q.get())
# print(q.get())
# print(q.get())
```

## Event

```
# import time
# from threading import Thread
# from threading import current_thread
#
# flag = False
#
#
# def task():
# print(f'{current_thread().name} ....')
# time.sleep(3)
# global flag
# flag = True
#
#
# def task1():
# while 1:
# time.sleep(1)
# print(f'{current_thread().name} .....')
# if flag:
# print(' ')
# return
#
# if __name__ == '__main__':
# t1 = Thread(target=task1,)
# t2 = Thread(target=task1,)
# t3 = Thread(target=task1,)
#
# t = Thread(target=task)
#
#
# t.start()
# t1.start()
# t2.start()
# t3.start()



import time
from threading import Thread
from threading import current_thread
from threading import Event

event = Event() # False
def task():
print(f'{current_thread().name} ....')
time.sleep(3)
event.set() # True

def task1():
print(f'{current_thread().name} ')
# event.wait() #  event True, True, . .
event.wait(1)
# , 1s ,event True, .
# , 1s ,event , .
print(f'{current_thread().name} ')
if __name__ == '__main__':
t1 = Thread(target=task1,)
t2 = Thread(target=task1,)
t3 = Thread(target=task1,)

t = Thread(target=task)


t.start()
t1.start()
t2.start()
t3.start()
```

##



##

```
# # import time
# #
# # def func1():
# #
# # for i in range(11):
# # yield
# # print(' %s ' % i)
# # time.sleep(1)
# #
# #
# # def func2():
# # g = func1()
# # #next(g)
# # for k in range(10):
# #
# # print(' , %s ' % k)
# # time.sleep(1)
# # next(g)
#
# # yield, func1 func2 ,
# # yield, +
# # func1()
# # func2()
#
#
# # :
# # import time
# #
# # def task1():
# # res = 1
# # for i in range(1,100000):
# # res += i
# #
# #
# # def task2():
# # res = 1
# # for i in range(1,100000):
# # res -= i
# #
# # start_time = time.time()
# # task1()
# # task2()
# # print(f' :{time.time()-start_time}') # :0.012000560760498047
#
#
# import time
#
#
# def task1():
# res = 1
# for i in range(1, 100000):
# res += i
# yield res
#
#
# def task2():
# g = task1()
# res = 1
# for i in range(1, 100000):
# res -= i
# next(g)
#
#
# start_time = time.time()
# task2()
# print(f' :{time.time() - start_time}') # :0.0260012149810791

#
# from greenlet import greenlet
# import time
#
# # ,
# # IO
# # .
# def eat(name):
#
# print('%s eat 1' %name) #2
# g2.switch('alex') #3
# time.sleep(3)
# print('%s eat 2' %name) #6
# g2.switch() #7
#
# def play(name):
# print('%s play 3' %name) #4
# g1.switch() #5
# print('%s play 4' %name) #8
#
# g1 = greenlet(eat)
# g2 = greenlet(play)
#
# g1.switch(' ') # 1
#
# # g2.switch('b1')
#
# # time.sleep(300)

# IO
#
# import gevent
# import time
# def eat(name):
# print('%s eat 1' %name) # 1
# # gevent.sleep(2)
# time.sleep(300)
# print('%s eat 2' %name)
#
# def play(name):
# print('%s play 1' %name) # 2
# # gevent.sleep(1)
# time.sleep(3)
# print('%s play 2' %name)
#
#
# g1 = gevent.spawn(eat, 'alex')
# g2 = gevent.spawn(play, name='taibai')
# # g1.join()
# # g2.join()
# # gevent.joinall([g1,g2])
# gevent.joinall([g1,g2])
# print(' ')



import threading
from gevent import monkey
monkey.patch_all() # IO .

import gevent #
import time
def eat():
print(f' 1:{threading.current_thread().getName()}')
print('eat food 1')
time.sleep(3) # mokey time sleep
print('eat food 2')

def play():
print(f' 2:{threading.current_thread().getName()}')
print('play 1')
time.sleep(1) # , I/O , gevent , 。
print('play 2')

g1=gevent.spawn(eat)
g2=gevent.spawn(play)
gevent.joinall([g1,g2])
print(f' :{threading.current_thread().getName()}')
```

전재 대상:https://www.cnblogs.com/Jacob-yang/p/11391694.html

좋은 웹페이지 즐겨찾기