- 使用异步函数实现并发
- 使用装饰器来进行重试
- 更具体的异常捕,利于后期维护
SEA | Swiss | PassOnline | Targrt Hunter | Target Net |
---|---|---|---|---|
DNN(ECfp4) | NB(ECfp4) | NN(MQN) + NB(ECfp4) | NN(Xfp) + NB(ECfp4) |
---|---|---|---|
NN(ECfp4) + NB(ECfp4) | NN(MQN) | NN(Xfp) | NN(ECfp4) |
pip install -r requirements.txt
python setup.py install
通过调用asyncio
运行异步函数
import asyncio
import json
import pandas as pd
from metaTarget import targetNet
if __name__ == "__main__":
smiles = "FC1=CC=C(CC2=NNC(=O)C3=CC=CC=C23)C=C1C(=O)N1CCN(CC1)C(=O)C1CC1"
jsonRes = asyncio.run(targetNet(smiles))
# print(jsonRes) # 结果为JSON字符串,用json和pandas转换成DataFrame
res = json.loads(jsonRes)
out = pd.DataFrame(res).T
print(out)
使用asyncio
的gather()
实现并发
import asyncio
import json
import pandas as pd
from metaTarget import targetNet, passOnline, targetHunter
async def main(smiles):
return await asyncio.gather(
targetNet(smiles),
passOnline(smiles),
targetHunter(smiles)
)
if __name__ == "__main__":
smiles = "FC1=CC=C(CC2=NNC(=O)C3=CC=CC=C23)C=C1C(=O)N1CCN(CC1)C(=O)C1CC1"
jsonRes = asyncio.run(main(smiles))
# print(jsonRes)
res = [json.loads(_) for _ in jsonRes]
out = [pd.DataFrame(_).T for _ in res]
# print(out)
请勿在Spyder、Jupyter或其他以IPython为运行环境的IDE中直接运行本程序,否则会抛出以下异常
RuntimeError: asyncio.run() cannot be called from a running event loop.
这是由于它们已经运行在事件循环(event loop)。
复制代码在IPython中运行
将asyncio.run()
替换成await
关键字
jsonRes = await targetNet(smiles)
The exception would be raised while running directly this code in Spyder, Jupyter or other IDE based on Ipython.
RuntimeError: asyncio.run() cannot be called from a running event loop.
Since IPython is already running an event loop, the asyncio.run()
should not be instead.
Paste the code block to Ipython to excute.
Use await
to instead all asyncio.run()
jsonRes = await targetNet(smiles)