-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.py
More file actions
86 lines (66 loc) · 1.78 KB
/
Copy pathprocess.py
File metadata and controls
86 lines (66 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from multiprocessing import Process,Pool
import os,time,random
def run_proc(name):
print('Run task %s (%s)...' % (name, os.getpid()))
start=time.time()
time.sleep(random.random()*10)
end=time.time()
print(name)
print('child process%s %s run(%s)ms' % (name, os.getpid(),end-start))
def callprecess():
p=Pool(20)
print('Parent process %s.' % os.getpid())
for x in range(4):
p.apply_async(run_proc, args=(x,))
p.apply_async(run_proc, args=(6,))
print('Child process will start.')
p.close()
p.join()
print('Child process end.')
import threading, multiprocessing
lock = threading.Lock()
balance=0
def change_it(n):
n=int(n)
global balance
lock.acquire()
balance=balance-n
balance=balance+n
lock.release()
def run(n):
for x in range(1000):
change_it(x)
# 创建全局ThreadLocal对象:
local_school = threading.local()
def tell():
name=local_school.name
print(name)
def chooseWhichTell(name):
local_school.name=name
tell()
t1=threading.Thread(target=chooseWhichTell,args=("LIU",))
t2=threading.Thread(target=chooseWhichTell,args=("GUO",))
t1.start()
t2.start()
t1.join()
t2.join()
#! /usr/bin/env python3
# -*- coding:utf-8 -*-
# task_master.py
import random, time, queue
from multiprocessing.managers import BaseManager
# 发送任务队列
task_queue = queue.Queue()
# 接受结果的队列
result_queue = queue.Queue()
# 从BaseManager继承的QueueManager
class QueueManage(BaseManager):
pass
# 把两个队列都注册到网络上,callable参数关联了Queue对象
QueueManage.register('get_task_queue', callable=lambda: task_queue)
QueueManage.register('get_result_queue', callable=lambda: result_queue)
# 绑定端口5000,设置验证码abc
manager = QueueManage(address=('',61000), authkey=b'abc')
# 启动Queue
server = manager.get_server()
server.serve_forever()