neumond / python-computer-craft

Pythonization of ComputerCraft Minecraft mod. Write Python instead Lua!
MIT License
67 stars 7 forks source link

Two way communication between a computer and another server #5

Open C-Bookie opened 3 years ago

C-Bookie commented 3 years ago

I've been attempting to understand how to I could wait on my own socket connection to another server, before triggering some computercraft command, however I keep running into issues with greenlet. As I understand it, I can't call any computercraft functions form another coroutine listening to a socket, otherwise I'll encounter the error 'Computercraft function was called outside context'. If I did find some way to wait for the signal in the current greenlet, then I assume I'd be blocking communication with the computercraft computer.

As a proof of concept, I just want to trigger a redstone signal using a websocket connection with a separate server. Is there any simple approach to this?

neumond commented 3 years ago

The problem is that it's always minecraft/lua side which asks python side to execute something. And this execution on python side should be as short as possible, similarly to actual mod mechanics: infinite loop in lua will load server CPU until the mod automatically kills it. There's no easy way to ask lua side to execute something, you have to distinguish between 1. several minecraft servers 2. several CC computers on server 3. each computer may have several threads of execution (all of these is actually a context). On top of that, CC computers may be disconnected from python side at the moment (e.g. turned off, or py program has been stopped).

For your case with redstone, we need a way to reuse existing http.websocket lua API without interference with python-computer-craft. This requires some work from my side. Since this repo gains some interest, I think I'll spare more time on it, but don't expect patches too soon.

C-Bookie commented 3 years ago

Hay, don't know if this is still intended development but I wouldn't mind having a crack at it on a fork if you've an idea of how I should approach it. I do have to ask, why the change from pure asyncio to greenlet?