質問編集履歴
1
すみませんいろいろ省いてしまいました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -9,6 +9,80 @@
|
|
9
9
|
### 実現したいこと
|
10
10
|
下の画像において、`decode`や`append`が白くなっています。ここを`multiprocessing`を使わないときのように色付けたいです。なお、この状態でもコード自体は正常に動きます(例えば`append`の場合、しっかり追加される)。`multiprocessing`を利用するときは特別な拡張機能、またはjsonファイルなどをいじる必要があるのでしょうか。よろしくお願いします。
|
11
11
|
|
12
|
+
### コードの構造
|
13
|
+
multiprocessingで使用する変数の宣言は`if __name__ == '__main__'`内にあります。また、receive_transaction以外の関数は省略し、その他ここには記載していない関数も多々あります。画像は、receive_transaction内の2個目のwhile文を示しています。
|
14
|
+
```python
|
15
|
+
def mine(...
|
16
|
+
def new_transaction(...
|
17
|
+
def receive_transaction(Mflag, transactions, recv_transactions, combined_transactions):
|
18
|
+
# cache-producer side = receive broadcasted data
|
19
|
+
# receive preInterest/transaction
|
20
|
+
|
21
|
+
with cefpyco.create_handle() as handle:
|
22
|
+
handle.register("ccnx:/preInterest/transaction")
|
23
|
+
while True:
|
24
|
+
info = handle.receive()
|
25
|
+
if info.is_succeeded and ("ccnx:/preInterest/transaction" in info.name) and (node_identifier not in info.name):
|
26
|
+
print("Receive transaction preInterest")
|
27
|
+
print(info.name)
|
28
|
+
namePrefix = re.findall("ccnx:/preInterest/transaction/(\w+)", info.name) # confirm sender's uuid
|
29
|
+
handle.send_interest("ccnx:/broadcast/transaction/{}/{}".format(namePrefix[0], node_identifier))
|
30
|
+
print("Sended transaction Interest")
|
31
|
+
# ccnx:/broadcast/transaction/sender's node id/my node id
|
32
|
+
|
33
|
+
while True:
|
34
|
+
tmpinfo = handle.receive()
|
35
|
+
if tmpinfo.is_succeeded and ("ccnx:/broadcast/transaction/{}/{}".format(namePrefix[0], node_identifier) in tmpinfo.name):
|
36
|
+
print("Receive transaction packet")
|
37
|
+
print(tmpinfo)
|
38
|
+
transaction = ast.literal_eval(tmpinfo.payload.decode("UTF-8"))
|
39
|
+
recv_transactions.append(transaction)
|
40
|
+
|
41
|
+
combined_transactions = transactions[:] + recv_transactions[:]
|
42
|
+
if len(combined_transactions) == 10:
|
43
|
+
print("----------recv_transaction Mflag----------")
|
44
|
+
Mflag.value = 1
|
45
|
+
|
46
|
+
break
|
47
|
+
def forward_transaction(...
|
48
|
+
def forward_block(...
|
49
|
+
def receive_block(...
|
50
|
+
|
51
|
+
if __name__ == '__main__':
|
52
|
+
|
53
|
+
manager = Manager()
|
54
|
+
|
55
|
+
# mine flag
|
56
|
+
Mflag = manager.Value('i', 0) # 1 means combined_transactions = 10
|
57
|
+
|
58
|
+
# list for sharing each other
|
59
|
+
transactions = manager.list()
|
60
|
+
recv_transactions = manager.list()
|
61
|
+
combined_transactions = manager.list()
|
62
|
+
|
63
|
+
chain = manager.list()
|
64
|
+
recv_chain = manager.list()
|
65
|
+
combined_chain = manager.list()
|
66
|
+
|
67
|
+
chain.append(blockchain.chain[-1])
|
68
|
+
|
69
|
+
jobs = [
|
70
|
+
Process(target=mine, args=(Mflag, transactions, recv_transactions,
|
71
|
+
combined_transactions, chain,)),
|
72
|
+
Process(target=new_transaction, args=(Mflag, transactions,
|
73
|
+
recv_transactions, combined_transactions,)),
|
74
|
+
Process(target=receive_transaction, args=(Mflag, transactions, recv_transactions,
|
75
|
+
combined_transactions,)),
|
76
|
+
Process(target=receive_block, args=(recv_chain,)),
|
77
|
+
]
|
78
|
+
|
79
|
+
for j in jobs:
|
80
|
+
j.start()
|
81
|
+
|
82
|
+
for j in jobs:
|
83
|
+
j.join()
|
84
|
+
```
|
85
|
+
|
12
86
|
### コード補完されない例
|
13
87
|
![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-12-07/eaae8fc7-c99a-48ba-b8b4-8f1d53a80f76.png)
|
14
88
|
|